UNPKG

keygentoolshed

Version:

Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3

47 lines (41 loc) 1.44 kB
// CAESAR CIPHER TIME :sob: import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; function caesarCipher(message, shift, encrypt = true) { const result = []; const adjustedShift = encrypt ? shift : -shift; for (let char of message) { if (char.match(/[a-z]/i)) { // Check if the character is a letter const code = char.charCodeAt(0); const base = code >= 65 && code <= 90 ? 65 : 97; // Uppercase or lowercase const newChar = String.fromCharCode(((code - base + adjustedShift + 26) % 26) + base); result.push(newChar); } else { result.push(char); // Non-letter characters are added unchanged } } return result.join(''); } const argv = yargs(hideBin(process.argv)) .option('message', { alias: 'm', description: 'The message to encrypt or decrypt', type: 'string', demandOption: true }) .option('shift', { alias: 's', description: 'The number of positions to shift the letters', type: 'number', demandOption: true }) .option('encrypt', { alias: 'e', description: 'Set to true to encrypt, false to decrypt', type: 'boolean', default: true }) .help() .argv; const result = caesarCipher(argv.message, argv.shift, argv.encrypt); console.log('Result:', result);