Skip to content

Instantly share code, notes, and snippets.

@datashaman
Last active August 24, 2020 09:08
Show Gist options
  • Save datashaman/d2f3b01196958b3adda9a62b5f75591c to your computer and use it in GitHub Desktop.
Save datashaman/d2f3b01196958b3adda9a62b5f75591c to your computer and use it in GitHub Desktop.
Asymmetric encryption using openssl_seal method from PHP with nodejs and AES256 cipher
const crypto = require('crypto');
const encrypt = (publicKey, message) => {
const key = Buffer.from(crypto.randomBytes(32), 'binary');
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes256', key, iv);
let data = cipher.update(message, 'utf8', 'base64');
data += cipher.final('base64');
const encryptedKey = crypto.publicEncrypt({
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PADDING
}, key);
return {
'data': data,
'env_key': encryptedKey.toString('base64'),
'iv': iv.toString('base64'),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment