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

81 lines (70 loc) 2.22 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; function railFenceEncrypt(message, rails) { const fence = Array.from({ length: rails }, () => []); let directionDown = false; // Direction flag for zigzag let row = 0; for (const char of message) { fence[row].push(char); if (row === 0 || row === rails - 1) { directionDown = !directionDown; } row += directionDown ? 1 : -1; // Move up or down } return fence.flat().join(''); } function railFenceDecrypt(message, rails) { const fence = Array.from({ length: rails }, () => []); let directionDown = false; // Direction flag for zigzag let row = 0; for (let i = 0; i < message.length; i++) { fence[row].push('*'); // Placeholder for characters if (row === 0 || row === rails - 1) { directionDown = !directionDown; } row += directionDown ? 1 : -1; // Move up or down } let index = 0; for (let r = 0; r < rails; r++) { for (let c = 0; c < fence[r].length; c++) { if (fence[r][c] === '*') { fence[r][c] = message[index++]; } } } let result = ''; row = 0; for (let i = 0; i < message.length; i++) { result += fence[row].shift(); // Get the next character if (row === 0 || row === rails - 1) { directionDown = !directionDown; } row += directionDown ? 1 : -1; // Move up or down } return result; } const argv = yargs(hideBin(process.argv)) .option('message', { alias: 'm', description: 'The message to encrypt or decrypt', type: 'string', demandOption: true }) .option('rails', { alias: 'r', description: 'The number of rails to use for encryption or decryption', 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 = argv.encrypt ? railFenceEncrypt(argv.message, argv.rails) : railFenceDecrypt(argv.message, argv.rails); console.log('Result:', result);