jsencrypt icon indicating copy to clipboard operation
jsencrypt copied to clipboard

Why is the result of each encryption different from the same content?

Open carveybunt opened this issue 2 years ago • 1 comments

version: 3.3.2

export function encryptViaPriPem() {
  const txt = '123'
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey)
  return encryptor.encrypt(txt)
}

And get false for decrypt.

carveybunt avatar Aug 24 '23 08:08 carveybunt

When encrypting with the private RSA key, JSEncrypt extracts the public RSA key from the private RSA key and performs a regular RSA encryption with the extracted public RSA key using the encryption scheme RSAES-PKCS1-v1_5. This scheme is characterized by a ciphertext that changes with each encryption, even when using the same key and the same plaintext.

Decryption must and can only be performed with the private key. If an attempt is made to decrypt with the public key, the decryption fails and a false is returned.

See here: https://jsfiddle.net/d7eap19u/

user163 avatar May 11 '25 18:05 user163