Skip to content

Instantly share code, notes, and snippets.

@btd
Created November 12, 2020 10:35
Show Gist options
  • Save btd/915985269cd2c98a17144a4660f45a09 to your computer and use it in GitHub Desktop.
Save btd/915985269cd2c98a17144a4660f45a09 to your computer and use it in GitHub Desktop.
JSEncypt to nodejs crypto and back
const JSEncrypt = require('node-jsencrypt');
const crypto = require('crypto');
const text = ' Some text ÜÄ';
const privateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF
z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5
rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM
V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe
aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil
psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz
uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876
-----END RSA PRIVATE KEY-----
`.trim();
const publicKey = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----
`.trim();
const jsEncryptEncrypt = (message) => {
const crypt1 = new JSEncrypt();
crypt1.setKey(publicKey);
const encrypted = crypt1.encrypt(message);
return encrypted;
};
const jsEncryptDecrypt = (message) => {
const crypt2 = new JSEncrypt();
crypt2.setKey(privateKey);
const decrypted = crypt2.decrypt(message);
return decrypted;
};
const privateKeyObj = crypto.createPrivateKey(privateKey);
const publicKeyObj = crypto.createPublicKey(publicKey);
const pad = (buf, length) => {
const missingLength = length - buf.length;
const randomNumber = 0xff;
const ps = Buffer.allocUnsafe(missingLength - 3).fill(randomNumber);
return Buffer.concat([Buffer.from([0x00, 0x02]), ps, Buffer.from([0x00]), buf]);
};
const unpad = (buf) => {
const prefixLength = 2;
const paddingEndIndex = buf.indexOf(0, prefixLength - 1);
return buf.slice(paddingEndIndex + 1);
};
const nodejsEncrypt = (message) => {
const paddedMessage = pad(Buffer.from(message, 'utf-8'), 128);
const result = crypto.publicEncrypt(
{ key: publicKeyObj, padding: crypto.constants.RSA_NO_PADDING },
paddedMessage,
);
return result.toString('base64');
};
const nodejsDecrypt = (message) => {
const result = crypto.privateDecrypt(
{ key: privateKeyObj, padding: crypto.constants.RSA_NO_PADDING },
Buffer.from(message, 'base64'),
);
return unpad(result).toString('utf-8');
};
const TEST_CASES = [
[jsEncryptEncrypt, jsEncryptDecrypt, 'jsencrypt-jsencrypt'],
[nodejsEncrypt, jsEncryptDecrypt, 'nodejs-jsencrypt'],
[jsEncryptEncrypt, nodejsDecrypt, 'jsencrypt-nodejs'],
[nodejsEncrypt, nodejsDecrypt, 'nodejs-nodejs'],
];
for (const [encrypt, decrypt, name] of TEST_CASES) {
console.log();
console.log('<<<<<', name, '>>>>>');
const encrypted = encrypt(text);
console.log('encrypted', encrypted);
const decrypted = decrypt(encrypted);
console.log('decrypted', decrypted);
if (text !== decrypted) {
console.log('FAIL');
} else {
console.log('PASS');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment