jsencrypt
jsencrypt copied to clipboard
add encryptOAEP to support encrypt with padding: OAEP and oaepHash: sha256
RSA_PKCS1_PADDING is not supported since node 18.20, this code add encryptOAEP to support encrypt text with padding: RSA_PKCS1_OAEP_PADDING and oaepHash: sha256
Encrypt with the public key:
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#pubkey').val());
var encrypted = encrypt.encryptOAEP($('#input').val());
Decrypt with the private key using nodejs:
import crypto from 'crypto';
export function privateDecrypt(privateKey: crypto.KeyLike, encryptedText: string) {
const encryptedBuffer = Buffer.from(encryptedText, 'base64');
const msgBuffer = crypto.privateDecrypt(
{ key: privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' },
encryptedBuffer
);
return String.fromCharCode.apply(null, msgBuffer);
}
var encrypt = new JSEncrypt();
encrypt.setPublicKey(pub)
var encrypted = encrypt.encryptOAEP(str)
console.log(encrypted)
var encrypt = new JSEncrypt(); encrypt.setPublicKey(pub) var encrypted = encrypt.encryptOAEP(str) console.log(encrypted)
You can use pandora-jsencrypt instead before publish