0

当使用 jsencrypt.min.js 加密和 java 解密时抛出异常

strp1:

后端 private_key 是:

MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJsgJYE6yJ25t8Nk6a50juAcwHDdhBLc1Lm3r9vFYZSqSrJKFs0ISodEnYkLwkoS8GrnwvXnmXngfbBHfzeWt0FwljpZiuAE/QM2FqF1q171dEJ/ZOE+epel6lCXsbf9K3k85nPj1u/i6IHgihJAE/eE55um5UwVG4XberTwZkghAgMBAAECgYBDRurdJChzt2MKXA64+ZYpfm67xGaOY7XiodakQvNyLXTd5CIFZC4mgkysbiwiQhqA0ukMCxjV8dWxCSEUc8of65d1gSvWr2lwo/WR+hTtPkMhxW1ONV5IzifOqz42MNezxNZPAT9FXyShBlxty0uKHpoAKN66Xpma0qaZ4k5aUQJBAMtV+aJykveLdeBlVSgpZEX+QKQj2xNIxc6rxkmiB+86l4PbD6ZrolWWadmODLSSsjz0FQiDSoyTnYnVze418R0CQQDDTaCDx7fxXkbvHxB7mKoXR2NHIvsQ+K3WMamBBdSTlaxoIsP+v1b4KxDgQEz9Qfnm5usiuHpf3Y7qS+Ip6mfVAkEAg7pCDAx/y6Dsj5/2KBuQm4uYov2bmoWlqDs+LUusKj5iFAjhvaWzOBFGp6tP7devYq+ZxLcRn7qt77K0Q2NsvQJBAIdngXr787a/U1Sj5+yY+owrLVR7ly+8CeHwCoHuheRQp/iG6fXruKNUUF35lqcbn+QLg0hOOGHQnS4psEK5RoUCQQCk1/A/kwaKW2Aj5zEapK+lHTV/JtWruhZJmN4qdRIopIRNKwCUlwpPrSSYBxwggNPSWqi98mX/pi3307iflj+a

后端 public_key 是:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbICWBOsidubfDZOmudI7gHMBw3YQS3NS5t6/bxWGUqkqyShbNCEqHRJ2JC8JKEvBq58L155l54H2wR383lrdBcJY6WYrgBP0DNhahdate9XRCf2ThPnqXpepQl7G3/St5POZz49bv4uiB4IoSQBP3hOebpuVMFRuF23q08GZIIQIDAQAB

step2:我使用vue.js导入jsencrypt.min.js加密代码,代码为:

VxxRfQ5QciBbmDHUFHk/tJEdZP7ypweW4fLiw7/nXrdFIrykj1+0Dcr6Uk6tskLoM2z7eyyAfXYygjiZiGzkazs2uJ3udZjlWaG2VBR9+yW7V5qJg0GEP1ir5BBIxBZrTTVIyruFeMuBnFWqjApin+eJWRGZ1EF+ZNgOh0/uM8xTIE7gg6ypmjdu0W1sHEgdcPcZrB7m7zoRi7Qgpj3b1tVafeE4FOS7ySDqr2HWByL1hSmqQGN3e8W/Fn0dnTSkByXnAqSkRN/peTphPkrzXGUBbNWHQT+WU701Y52hoqIUO+IHOHk+UZlDpAQSEHs6rHPXXnPOui66eghI3j9C0A==

第3步:使用java解密该代码,然后抛出Excetpion

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:383)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:294)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2222)
at com.javaweb.util.core.RsaUtil.decrypt(RsaUtil.java:197)
at com.javaweb.util.core.RsaUtil.main(RsaUtil.java:218)

我的java解密代码是

 public static String decrypt(String data, PrivateKey privateKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");//RSA/ECB/NoPadding//RSA/ECB/PKCS1Padding
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] dataBytes = Base64.decodeBase64(data);
    int inputLen = dataBytes.length;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int offset = 0;
    byte[] cache;
    int i = 0;
    while (inputLen - offset > 0) {
        if (inputLen - offset > MAX_DECRYPT_BLOCK) {
            cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
        } else {
            cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
        }
        out.write(cache, 0, cache.length);
        i++;
        offset = i * MAX_DECRYPT_BLOCK;
     }
     byte[] decryptedData = out.toByteArray();
     out.close();
     return new String(decryptedData,"UTF-8");
}

MAX_DECRYPT_BLOCK = 128

我发现step2 code字节数组长度是256,但是如果解密这段代码

f/ghwhailFH1GUbvvm2FpvaZttChJgsSorUx8Wy83PaC2ZorfMdu3Lz5Px56GLbX5VbKtdRTdRuC+yCGK8RZICUxJpTdblN8yoKUI2LdxPFnc7IHlRWN60xkww1bQ3c9QyT5IKYe7TKOx7SGmUXIi0ec/QMEklEYUUG8YrmJ7Xo=

我可以得到正确的结果,然后我更改了 MAX_DECRYPT_BLOCK = 256,但仍然没有用,然后我看到有人说“jsencrypt 使用 pkcs1 规则,java 使用 pkcs8 规则”,我尝试了 Cipher.getInstance("RSA/ECB/PKCS1Padding") 但仍然没用。

你有什么想法来解决这个问题

4

0 回答 0