Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kingller
Copy link
Contributor

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

@FIGHTING-TOP
Copy link

FIGHTING-TOP commented Dec 18, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants