Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Created May 27, 2020 10:14
Show Gist options
  • Save sohalloran/f7623e1afea5f9bd150e605ca8d14cf0 to your computer and use it in GitHub Desktop.
Save sohalloran/f7623e1afea5f9bd150e605ca8d14cf0 to your computer and use it in GitHub Desktop.
const crypto = require("crypto");
const algorithm = "aes-256-cbc";
const ENCRYPTION_KEY = Buffer.from(
"b7zfYEKhkmVrl9P3yGN/p+3AOkjWK5D6s2JOJzW6lpo=",
"base64",
);
const IV_LENGTH = 16;
function encrypt(text) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv(
algorithm,
Buffer.from(ENCRYPTION_KEY, "hex"),
iv,
);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString("hex") + ":" + encrypted.toString("hex");
}
function decrypt(text) {
let textParts = text.split(":");
let iv = Buffer.from(textParts.shift(), "hex");
let encryptedText = Buffer.from(textParts.join(":"), "hex");
let decipher = crypto.createDecipheriv(
algorithm,
Buffer.from(ENCRYPTION_KEY, "hex"),
iv,
);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
let a = encrypt("node-cheat");
let b = decrypt(a);
console.log(a, b);
function decryptApex(t) {
const text = Buffer.from(t, "base64");
let btext = Buffer.from(text, "hex");
let iv = btext.slice(0, 16);
let encryptedText = btext.slice(16);
let decipher = crypto.createDecipheriv(
algorithm,
Buffer.from(ENCRYPTION_KEY, "hex"),
iv,
);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
let c = decryptApex(
"shZiijRRaNy8HSDcZeAwYELhBltiBrInSz0MH/F7Ol23VHmJmm3QD1xOMQqaN4FP",
);
console.log("Apex Crypto with Managed IV decrypted : " + c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment