Skip to content

get-your-knowledge-here/caesar-cipher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GYKH - caesar-cipher

One of the simplest forms of encryption

Install

pnpm add @gykh/caesar-cipher

Requires Node.js 18 or newer.

Usage

Version 3 implements a classic Caesar cipher for ASCII letters. A-Z and a-z shift with wraparound, while punctuation, whitespace, digits, and non-letter characters are preserved.

Encrypt and decrypt strings:

const { encryptString, decryptString } = require("@gykh/caesar-cipher");

const str = "Hello, World! 123";

const encrypted = encryptString(str, 3);
console.log(encrypted); // Khoor, Zruog! 123

const decrypted = decryptString(encrypted, 3);
console.log(str === decrypted); // true

Encrypt and decrypt buffers:

const { encrypt, decrypt } = require("@gykh/caesar-cipher");
const { readFile } = require("fs/promises");

const buffer = await readFile(inputFile);

const encrypted = encrypt(buffer, 3);

const decrypted = decrypt(encrypted, 3);
console.log(buffer.equals(decrypted)); // true

Encrypt and decrypt streams. Use this for large data or files:

const { EncryptTransform, DecryptTransform } = require("@gykh/caesar-cipher");
const fs = require("fs");
const { pipeline } = require("stream/promises");

await pipeline(
  fs.createReadStream(inputFile),
  new EncryptTransform(3),
  new DecryptTransform(3),
  fs.createWriteStream(outputFile)
);

API

caesar-cipher

.encryptString(input, key)

input

Type: string
Required

key

Type: number
Required

key should be an integer between 0-25

.decryptString(input, key)

input

Type: string
Required

key

Type: number
Required

key should be an integer between 0-25

.encrypt(input, key)

input

Type: Buffer
Required

key

Type: number
Required

key should be an integer between 0-25

.decrypt(input, key)

input

Type: Buffer
Required

key

Type: number
Required

key should be an integer between 0-25

new EncryptTransform(key)

key

Type: number
Required

key should be an integer between 0-25

new DecryptTransform(key)

key

Type: number
Required

key should be an integer between 0-25

Understand Caesar Cipher

The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet. Learn more

Developer

"Buy Me A Coffee"

License

MIT © 2021 get-your-knowledge-here

About

The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.

Topics

Resources

License

Stars

Watchers

Forks

Contributors