UNPKG

crypto-keygen-suite

Version:

Key generation utilities for cryptographic operations. YES I RENAMED IT OMG BASES AND PROTOCOLS!!! See its folder for all <3

50 lines (44 loc) 1.54 kB
// what am i doing with my life import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; function vigenereCipher(message, keyword, encrypt = true) { const result = []; const keywordRepeater = keyword.repeat(Math.ceil(message.length / keyword.length)).toUpperCase(); for (let i = 0, j = 0; i < message.length; i++) { const char = message[i]; if (char.match(/[a-z]/i)) { const code = char.charCodeAt(0); const base = code >= 65 && code <= 90 ? 65 : 97; const shift = keywordRepeater[j].charCodeAt(0) - 65; const newChar = String.fromCharCode(((code - base + (encrypt ? shift : -shift) + 26) % 26) + base); result.push(newChar); j++; } else { result.push(char); } } 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('keyword', { alias: 'k', description: 'The keyword used for encryption or decryption', type: 'string', demandOption: true }) .option('encrypt', { alias: 'e', description: 'Set to true to encrypt, false to decrypt', type: 'boolean', default: true }) .help() .argv; const result = vigenereCipher(argv.message, argv.keyword, argv.encrypt); console.log('Result:', result);