Skip to content

Instantly share code, notes, and snippets.

@pyperanger
Created March 20, 2020 16:09
Show Gist options
  • Save pyperanger/b97243dddff3ac99c9067f9a000dac96 to your computer and use it in GitHub Desktop.
Save pyperanger/b97243dddff3ac99c9067f9a000dac96 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const NodeRSA = require('node-rsa');
const cryptokey = ""; // private key
const keyoptions = {
encryptionScheme: 'pkcs1'
};
const privateKey = fs.readFileSync(cryptokey, 'utf8');
const key = new NodeRSA(privateKey);
key.setOptions(keyoptions);
function encrypt(text){
console.log("Encrypting text: "+text);
console.log("Encrypted text: "+key.encrypt("text", "base64"))
}
function decrypt(text){
console.log("Decrypting text: "+text);
console.log("Encrypted text: "+key.decrypt(text, "utf-8").toString('utf-8'))
}
if (process.argv.length <= 3) {
console.log("Usage: node nodersa-sample.js [0:1] 'text'\n0 - encrypt\n1 - decrypt");
process.exit(-1);
}
switch(process.argv[2]) {
case "0":
encrypt(process.argv[3]);
break;
case "1":
decrypt(process.argv[3]);
break;
default:
console.log("Invalid number\n");
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment