Skip to content

Instantly share code, notes, and snippets.

@saii9
Created April 16, 2019 15:37
Show Gist options
  • Save saii9/28bac4ebf4ada366f3ab3b86a949a252 to your computer and use it in GitHub Desktop.
Save saii9/28bac4ebf4ada366f3ab3b86a949a252 to your computer and use it in GitHub Desktop.
end to end encryption to encrypt body with symmetric key and meta data in header with asymmetric key
const crypto = require('crypto');
const path = require("path");
const fs = require("fs");
const encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
const absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
const publicKey = fs.readFileSync(absolutePath, "utf8");
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(toEncrypt));
return encrypted.toString("base64");
};
const decryptStringWithRsaPrivateKey = function(toDecrypt, relativeOrAbsolutePathtoPrivateKey) {
const absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
const privateKey = fs.readFileSync(absolutePath, "utf8");
const buffer = Buffer.from(toDecrypt, "base64");
const decrypted = crypto.privateDecrypt(privateKey, buffer);
console.log("FE - decryptStringWithRsaPrivateKey");
return decrypted.toString("utf8");
};
const encrypt = function(text, algorithm, key, iv) {
console.log(algorithm, Buffer.from(iv, "base64"), Buffer.from(iv, "base64").length)
let cipher = crypto.createCipheriv(algorithm,
Buffer.from(key, "base64"),
Buffer.from(iv, "base64"));
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString('hex');
}
const decrypt = function(text, algorithm, key, iv) {
let encryptedText = Buffer.from(text, 'hex');
let decipher = crypto.createDecipheriv(algorithm,
Buffer.from(key, "base64"),
Buffer.from(iv,"base64"));
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
const algorithms = [
'aes-256-cbc',
'aes-256-cfb',
'aes-256-ctr',
'aes-256-ofb',
'aes256',
'aes-128-xts',
];
const E2EEncryption = function(body){
var hw_meta = {
algorithm: algorithms[Math.floor(Math.random()*algorithms.length)],
key: crypto.randomBytes(32).toString('base64'),
iv: crypto.randomBytes(16).toString('base64'),
};
const hw = encrypt(body,
hw_meta.algorithm,
hw_meta.key,
hw_meta.iv);
const enc_body_meta = encryptStringWithRsaPublicKey(JSON.stringify(hw_meta),
'certs/server_public.key')
return {meta: enc_body_meta, content: hw};
}
const E2EDecryption = function(data) {
const meta = data.meta;
const encText = data.content;
const dec_body_meta = decryptStringWithRsaPrivateKey(
meta,
'certs/server_private.key');
const hw_meta = JSON.parse(dec_body_meta)
return decrypt(encText,
hw_meta.algorithm,
Buffer.from(hw_meta.key, "base64"),
Buffer.from(hw_meta.iv, "base64")
);
}
let encObj = E2EEncryption("Some Serios Stuff")
decTxt = E2EDecryption(encObj)
console.log(decTxt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment