Java RSA实现:javax.crypto.BadPaddingException

布拉宗尼

我知道以前曾问过这个问题,我不会再问我是否不明白我所读的内容-我正在实现RSA算法的简单演示-加密消息,解密并打印输出,但控制台显示代码中引发了BadPaddingException:

这是代码和控制台输出:

代码

public class RSA {
public static void main (String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);   // 1024 or 2048
    KeyPair kp = kpg.generateKeyPair();
    Key publicKey = kp.getPublic();
    Key privateKey = kp.getPrivate();
    Cipher cipher = generateCipher();
    String data = "abcdefghijklmnop\0\0\0";

    System.out.println("Plaintext: " + data);
    byte[] ciphertext = rsaEncrypt(data.getBytes(), publicKey, cipher);
    System.out.println("Ciphertext: " + ciphertext);
    byte[] plaintext = rsaDecrypt(data.getBytes(), privateKey, cipher);
    System.out.println("Decrypted Plaintext: " + plaintext);
}

public static Cipher generateCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("RSA");
    return cipher;
}

public static byte[] rsaEncrypt(byte[] data, Key publicKey, Cipher cipher) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
      cipher.init(Cipher.ENCRYPT_MODE, publicKey);
      byte[] cipherData = cipher.doFinal(data);
      return cipherData;
}

public static byte[] rsaDecrypt(byte[] data, Key privateKey, Cipher cipher) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
      cipher.init(Cipher.DECRYPT_MODE, privateKey);
      byte[] cipherData = cipher.doFinal(data); // error line: at learning.RSA.rsaDecrypt(RSA.java:43)

      return cipherData;
}
}

安慰

Plaintext: abcdefghijklmnop
Ciphertext: [B@346cd0f9
Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:329)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:272)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at learning.RSA.rsaDecrypt(RSA.java:43)
at learning.RSA.main(RSA.java:26)

同样,不要求解决方案,而是希望对概念填充进行解释-如果您有足够的资源来实现RSA的示例代码,我也很喜欢这些建议。

谢谢 :)

吉姆·加里森

    byte[] plaintext = rsaDecrypt(data.getBytes(), privateKey, cipher);

应该

    byte[] plaintext = rsaDecrypt(ciphertext, privateKey, cipher);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

javax.crypto.BadPaddingException

RSA解密期间的javax.crypto.BadPaddingException

javax.crypto.BadPaddingException:解密错误使用Java RSA加密时,

解密RSA数据Java时出错:javax.crypto.BadPaddingException:解密错误

我有javax.crypto.BadPaddingException:RSA密码术

javax.crypto.BadPaddingException:解密错误

java aes javax.crypto.BadPaddingException:给定的最终块未正确填充

Python与Kotlin AES通信(javax.crypto.BadPaddingException)

javax.crypto.BadPaddingException:填充块损坏的异常

AES加密期间出现javax.crypto.BadPaddingException错误

javax.crypto.BadPaddingException:当我尝试使用私钥解密RSA字符串时发生解密错误

使用SSL连接到Tibco EMS时出现javax.crypto.BadPaddingException

javax.crypto.BadPaddingException:给定的最终块未正确填充-AES / CBC / PKCS5PADDING

javax.crypto.BadPaddingException:给定最终块未正确填充-奇怪的错误

为什么我收到javax.crypto.BadPaddingException(AES / CBC / PKCS5Padding)

RSA OAEP,Golang加密,Java Decrypt -BadPaddingException:解密错误

正在获取“原因:无法解密安全内容条目:javax.crypto.BadPaddingException:给定的最终块未正确填充”

javax.crypto.BadPaddingException:解密错误:仅从数据库解密到一个已经加密的字符串时

Java中的RSA BadPaddingException-在Android中加密在JRE中解密

使用RSA解密时出现BadPaddingException

Java AES GCM javax.crypto.AEADBadTagException:标记不匹配

javax.crypto.Cipher对RSA使用哪种填充

从其他客户端解密RSA时出现BadPaddingException

Java AES:没有安装的提供程序支持此密钥:javax.crypto.spec.SecretKeySpec

使用javax.crypto.Cipher和Java.security.Signature签名

Java javax.crypto和PHP openssl_decrypt不相同

Maven构建失败,因为“线程“主”中的异常java.lang.NoClassDefFoundError:javax / crypto / spec / Sec retKeySpec”

我如何解决此错误线程“主”中的异常java.lang.NoClassDefFoundError:javax / crypto / SecretKey

错误decrjavax.crypto.BadPaddingException:给定最终块未正确填充。如果在解密过程中使用了错误的密钥,则会出现此类问题