jsencrypt icon indicating copy to clipboard operation
jsencrypt copied to clipboard

add encryptOAEP to support encrypt with padding: OAEP and oaepHash: sha256

Open kingller opened this issue 1 year ago • 2 comments

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);
}

kingller avatar Nov 25 '24 08:11 kingller

            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(pub)
            var encrypted = encrypt.encryptOAEP(str)
            console.log(encrypted)

FIGHTING-TOP avatar Dec 18 '24 12:12 FIGHTING-TOP

            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(pub)
            var encrypted = encrypt.encryptOAEP(str)
            console.log(encrypted)

You can use pandora-jsencrypt instead before publish

kingller avatar Jan 24 '25 00:01 kingller