UNPKG

crypto-keygen-suite

Version:

Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3

74 lines (65 loc) 2.56 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import crypto from 'crypto'; // Simulated Homomorphic Encryption System class HomomorphicEncryption { constructor() { this.key = crypto.randomBytes(32); // Secret key for encryption } encryptData(data) { const nonce = crypto.randomBytes(12); const cipher = crypto.createCipheriv('aes-256-gcm', this.key, nonce); const encrypted = Buffer.concat([cipher.update(data.toString(), 'utf8'), cipher.final()]); const authTag = cipher.getAuthTag(); return Buffer.concat([nonce, authTag, encrypted]).toString('hex'); } homomorphicAddition(encrypted1, encrypted2) { const buffer1 = Buffer.from(encrypted1, 'hex'); const buffer2 = Buffer.from(encrypted2, 'hex'); // Simulated Homomorphic Operation: XOR (Simple Addition) const computed = Buffer.from(buffer1.map((byte, index) => byte ^ buffer2[index])); // Bitwise XOR return computed.toString('hex'); } } // CLI Setup async function main() { const argv = yargs(hideBin(process.argv)) .option('mode', { alias: 'm', type: 'string', choices: ['encrypt', 'add'], describe: 'Encrypt data or perform homomorphic addition', }) .option('data', { alias: 'd', type: 'string', describe: 'Data to encrypt', }) .option('data2', { alias: 'd2', type: 'string', describe: 'Second encrypted data for addition', }) .help() .argv; const homomorphic = new HomomorphicEncryption(); if (argv.mode === 'encrypt') { if (!argv.data) { console.error("❌ Encryption requires input data!"); return; } const encryptedData = homomorphic.encryptData(argv.data); console.log(`🔐 Encrypted Data (Hex): ${encryptedData}`); } else if (argv.mode === 'add') { if (!argv.data || !argv.data2) { console.error("❌ Homomorphic addition requires two encrypted values!"); return; } const computedResult = homomorphic.homomorphicAddition(argv.data, argv.data2); console.log(`➕ Homomorphic Addition Result (Hex): ${computedResult}`); } else { console.error("❌ Invalid mode. Use --mode encrypt | add"); } } console.log("🚀 Debug: Running main function..."); main();