jsencrypt returns null decrypting string from encrypt function

1.7k views Asked by At

I am playing with jsencrypt and noticed something very strange. Here is my code. The encrypt and decrypt works if they are in a variable. But when I console log the enc and then copy the value into a string, the decryption returns null. Does anyone know what I'm doing wrong?

const crypt = new JSEncrypt();
  crypt.setPublicKey = `-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnL4SAkXXmeub2DccgTT4v9EQr
  vkvqPQPzRPOeIL525tQd7vg1BlgxxnoS4oDn0G3JlzqI6y/VIaZy6QrIo3s5R2A9
  IiymPkeVOih4V5QeNSAO8tmUbEX4uEFWQDyObMeyF44R7fUoyUCcC/WdKezwcJlg
  q6dXIFJOyEzwJx0MPQIDAQAB
  -----END PUBLIC KEY-----`;

  crypt.setPrivateKey = `-----BEGIN PRIVATE KEY-----
  MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKcvhICRdeZ65vYN
  xyBNPi/0RCu+S+o9A/NE854gvnbm1B3u+DUGWDHGehLigOfQbcmXOojrL9UhpnLp
  CsijezlHYD0iLKY+R5U6KHhXlB41IA7y2ZRsRfi4QVZAPI5sx7IXjhHt9SjJQJwL
  9Z0p7PBwmWCrp1cgUk7ITPAnHQw9AgMBAAECgYEAl4kAZrBx1wiLE3cFiAe7WCfl
  Kmj/0j6ft/5FeoxfVtJ+bOtPWnB3vBYUS8213WgUOuayDaxu3JX1kq4cMiPL6XWC
  gznYdRDrO/92E5OyOjmrAGzItDv/SIoBGv68f/kxYC989H+yd7aCqF8SfRlR0ESi
  PnLLSYZfRsB4oLeSpkECQQDZdMLOZ3uiRwBvPV79mqfmME2OpB5VssAS9GO+giF3
  c8jibpre2wV/xWZ4/ACeXmFRgejVp09JEvF2OARhgSyVAkEAxNGxzJH22E0ZT3kq
  W7L7o8mkCQVjLK99dMaagUg+VpKk+4Y02hBStLP4xnIHt7NfpQ7r+cebk3ZeH5Ep
  hO7PCQJBALcKhpPxHWuKcn4wXY4BKYUkcZQ452Phk7YgyvjoVr1S0+xth6VBHU7g
  3ZHHJcoozagrPs7hB2cOL/v/yQg+EEUCQH5Ila5OkXWjsnO9qKIBq1giDvAC1ulb
  CEIfFA+TjPv7R4RjThIEmyx6tUqO68jAfMPpWRuBKZ9qOWZDu8IhgBECQCZTubwP
  zZ+9Kntj9BcoRjYYMeWK8voPzS4n1S8PK3249e7GdfX/Z7OcsvV9gYJ7CZ6W41fo
  wa3MnlkEivo3mrA=
  -----END PRIVATE KEY-----`;

  const text = "some data";
  const enc = crypt.encrypt(text);
  const dec = crypt.decrypt(enc);
  console.log('enc:', enc);
  const temp = 'EutZlbeFV2RTG8mk6xUMi/ceN1hmj/xFre+7GkmFMBX5ZTl+rpQlJfZC9uoBBO8ywoi0YceF+lXgpDlvMxUTKnK7nLjt2L4AWEBCXT5XNKdK83N4kkMgZkZocoTD/I7mBBJQ7ZHRgtMRIoPoVBKz2HhGaK2tq3U1lDtkDACuZ3k= '; 
  // temp is cut and paste from previous console log

  const dec2 = crypt.decrypt(temp);

  console.log('dec2:', dec2); // prints null
1

There are 1 answers

0
Terry Lennox On BEST ANSWER

You need to call the functions setPublicKey and setPrivateKey rather than assigning them.

Since you weren't actually setting the keys, the previous output would have been encoded with a different key, hence why it won't decode.

The previous example of an encrypted string 'EutZlbeFV2RTG8mk6xUMi/ceN1h...' you used also won't decode, since it was encoded using another key (which has probably been lost.)

But if you copy and paste from now on, the text will decode.

Note that you only need to set the private key, since the private key contains the public key parameters

const crypt = new JSEncrypt();

// Set private key
crypt.setPrivateKey(`-----BEGIN PRIVATE KEY-----
  MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKcvhICRdeZ65vYN
  xyBNPi/0RCu+S+o9A/NE854gvnbm1B3u+DUGWDHGehLigOfQbcmXOojrL9UhpnLp
  CsijezlHYD0iLKY+R5U6KHhXlB41IA7y2ZRsRfi4QVZAPI5sx7IXjhHt9SjJQJwL
  9Z0p7PBwmWCrp1cgUk7ITPAnHQw9AgMBAAECgYEAl4kAZrBx1wiLE3cFiAe7WCfl
  Kmj/0j6ft/5FeoxfVtJ+bOtPWnB3vBYUS8213WgUOuayDaxu3JX1kq4cMiPL6XWC
  gznYdRDrO/92E5OyOjmrAGzItDv/SIoBGv68f/kxYC989H+yd7aCqF8SfRlR0ESi
  PnLLSYZfRsB4oLeSpkECQQDZdMLOZ3uiRwBvPV79mqfmME2OpB5VssAS9GO+giF3
  c8jibpre2wV/xWZ4/ACeXmFRgejVp09JEvF2OARhgSyVAkEAxNGxzJH22E0ZT3kq
  W7L7o8mkCQVjLK99dMaagUg+VpKk+4Y02hBStLP4xnIHt7NfpQ7r+cebk3ZeH5Ep
  hO7PCQJBALcKhpPxHWuKcn4wXY4BKYUkcZQ452Phk7YgyvjoVr1S0+xth6VBHU7g
  3ZHHJcoozagrPs7hB2cOL/v/yQg+EEUCQH5Ila5OkXWjsnO9qKIBq1giDvAC1ulb
  CEIfFA+TjPv7R4RjThIEmyx6tUqO68jAfMPpWRuBKZ9qOWZDu8IhgBECQCZTubwP
  zZ+9Kntj9BcoRjYYMeWK8voPzS4n1S8PK3249e7GdfX/Z7OcsvV9gYJ7CZ6W41fo
  wa3MnlkEivo3mrA=
  -----END PRIVATE KEY-----`);

const text = "some data";
const enc = crypt.encrypt(text);
const dec = crypt.decrypt(enc);
console.log('enc:', enc);
const temp = 'T9tsyIGc+fFea7GWKHiJLOVbZf3vqiVEy/qBK37hxrGlsEQWh2HgD5+76XVqkmz6yU6w77CMX3wsRtH3KeD2NY4wxsb18JwcO4yJcaFL+ZwJ7WS8PuGY4YI5UxW3v2lwkwZ2x8GO/lE25pPJ4+f5bdeM4Yk5KWt6hV3z/yXCwvc=';
const dec2 = crypt.decrypt(temp);
console.log('dec2:', dec2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsencrypt/2.3.1/jsencrypt.min.js" integrity="sha512-zDvrqenA0eFJZCxBsryzUZcvihvNlEXbteMv62yRxdhR4s7K1aaz+LjsRyfk6M+YJLyAJEuuquIAI8I8GgLC8A==" crossorigin="anonymous"></script>