jsencrypt returns null 从加密函数解密字符串

jsencrypt returns null decrypting string from encrypt function

我正在玩 jsencrypt 并注意到一些非常奇怪的事情。这是我的代码。如果加密和解密在变量中,则它们有效。但是当我控制台登录 enc 然后将值复制到一个字符串中时,解密 returns 为空。有谁知道我做错了什么?

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

您需要调用函数 setPublicKey 和 setPrivateKey 而不是分配它们。

由于您实际上并未设置密钥,因此之前的输出会使用不同的密钥进行编码,因此无法解码。

您之前使用的加密字符串示例 'EutZlbeFV2RTG8mk6xUMi/ceN1h...' 也无法解码,因为它是使用另一个密钥(可能已经丢失)编码的。

但如果您从现在开始复制粘贴,文本将解码。

注意你只需要设置私钥,因为私钥包含public个密钥参数

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>