Last active
August 24, 2020 09:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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