A fast, zero-dependency Caesar shift cipher implementation in Node.js supporting Strings, Buffers, Streams, ROT13, and Cryptanalysis Cracking.
- 🚀 Zero Dependencies: Pure native Node.js implementation.
- ⚡ High Performance: Optimized character code mapping with minimal memory allocation.
- 🔄 Multi-Format Support: Encrypt and decrypt Strings, Buffers, and Node.js Streams.
- 📦 Dual ESM & CommonJS: Full support for both
importandrequire. - 📘 TypeScript Included: Full type definitions (
index.d.ts) with rich autocomplete. - 🕵️ Cracking & ROT13: Built-in
rot13(str)andcrack(str)/bruteForce(str)cryptanalysis helpers.
pnpm add @gykh/caesar-cipher
# or
npm install @gykh/caesar-cipher
# or
yarn add @gykh/caesar-cipherRequires Node.js 18 or newer.
The Caesar cipher shifts standard ASCII letters (A-Z and a-z) with wraparound modulo 26, while punctuation, whitespace, digits, and special characters are preserved.
// ESM
import { encryptString, decryptString, rot13, crack } from "@gykh/caesar-cipher";
// CommonJS
// const { encryptString, decryptString, rot13, crack } = require("@gykh/caesar-cipher");
const message = "Hello, World! 123";
// Encrypt with a shift of 3
const encrypted = encryptString(message, 3);
console.log(encrypted); // "Khoor, Zruog! 123"
// Decrypt back with shift 3
const decrypted = decryptString(encrypted, 3);
console.log(decrypted); // "Hello, World! 123"
// ROT13 shortcut (shift of 13)
const rot13Text = rot13(message);
console.log(rot13(rot13Text) === message); // true
// Brute-force crack an unknown ciphertext
const allPossibleShifts = crack(encrypted);
// Returns an array of 25 possible shifts: [{ shift: 1, text: '...' }, ..., { shift: 3, text: 'Hello, World! 123' }, ...]import { encrypt, decrypt } from "@gykh/caesar-cipher";
import { readFile } from "fs/promises";
const buffer = await readFile("sample.txt");
const encryptedBuffer = encrypt(buffer, 3);
const decryptedBuffer = decrypt(encryptedBuffer, 3);
console.log(buffer.equals(decryptedBuffer)); // trueFor files or streams exceeding 1000 characters/bytes, use the streaming transform classes:
import { EncryptTransform, DecryptTransform } from "@gykh/caesar-cipher";
import fs from "fs";
import { pipeline } from "stream/promises";
// Encrypt a large file via stream pipeline
await pipeline(
fs.createReadStream("large-input.txt"),
new EncryptTransform(3),
fs.createWriteStream("large-encrypted.txt")
);
// Decrypt stream
await pipeline(
fs.createReadStream("large-encrypted.txt"),
new DecryptTransform(3),
fs.createWriteStream("large-decrypted.txt")
);Encrypts a plaintext string.
str(string, max 1000 chars): Plaintext string to encrypt.key(number, 0–25): Integer shift amount.- Returns:
string
Decrypts a ciphertext string.
str(string, max 1000 chars): Ciphertext string to decrypt.key(number, 0–25): Integer shift amount used during encryption.- Returns:
string
Convenience utility to encode or decode text using the ROT13 cipher (shift 13).
str(string, max 1000 chars): Input string.- Returns:
string
Generates all 25 possible Caesar cipher shift permutations for cryptanalysis.
str(string, max 1000 chars): Encrypted string.- Returns:
Array<{ shift: number, text: string }>
Encrypts a byte buffer.
buffer(Buffer, max 1000 bytes): Input buffer.key(number, 0–25): Integer shift amount.- Returns:
Buffer
Decrypts an encrypted byte buffer.
buffer(Buffer, max 1000 bytes): Input buffer.key(number, 0–25): Integer shift amount.- Returns:
Buffer
A Node.js stream.Transform subclass for encrypting stream chunks.
key(number, 0–25): Shift key.
A Node.js stream.Transform subclass for decrypting stream chunks.
key(number, 0–25): Shift key.
Note
encryptString, decryptString, encrypt, decrypt, rot13, and crack enforce an input limit of 1000 characters/bytes to protect against unbounded memory spikes in non-streaming workloads. For larger data or file transfers, pipe through EncryptTransform / DecryptTransform.
You can try the interactive web demo directly in your browser by opening docs/index.html.
- Sylvester Das — Website • Buy Me A Coffee
MIT © 2021-2026 get-your-knowledge-here