当前位置: 首页 > 知识库问答 >
问题:

为什么我从RSA解密得到一个BadPadding异常?

荣德厚
2023-03-14

我用RSA算法加密和解密。当我加密一个字符串时,它工作正常。当我解密时,我得到一个错误。下面,我发布我的代码。

public final String modulusString ="..............";
public final String publicExponentString = "AQAB";

/* Encryption */
byte[] modulebytes = Base64.decode(modulusString);
byte[] exponentbytes = Base64.decode(publicExponentString);
BigInteger module = new BigInteger(1,modulebytes);
BigInteger publicexponent = new BigInteger(1,exponentbytes);
RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(module, publicexponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = EncryptionValue.getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String encryptedString = Base64.encode(cipherData);

return encryptedString;

/* Decryption */
byte[] modulebytes = Base64.decode(modulusString);
byte[] exponentbytes = Base64.decode(publicExponentString);

BigInteger modulus = new BigInteger(1, modulebytes );
BigInteger exponent = new BigInteger(1, exponentbytes);

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);

byte[] base64String = Base64.decode(DecryptionValue);
byte[] plainBytes = new String(base64String).getBytes("UTF-8");
plainBytes = cipher.update(plainBytes);
byte[] values = cipher.doFinal(plainBytes);

return new String(values, "UTF-8");
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
  at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
  at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
  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:2121)
  at cryptocodefinal.CryptoCodeFinal.DecryptionValue(CryptoCodeFinal.java:79)
  at cryptocodefinal.CryptoCodeFinal.main(CryptoCodeFinal.java:148)

共有1个答案

程祺
2023-03-14

您似乎正在使用公钥解密。那不行。您需要使用与用于加密的公共指数一起使用的私有指数进行解密。

没有私钥就无法解密。这就是非对称加密的要点。

 类似资料:
  • 我有一个启动屏幕,它运行一个从API下载数据的。在该任务的上,我运行下一个来发送存储的电子邮件。完成后,我需要一个弹出一个ok按钮,以便用户知道下载完成。我使用了这个SO问题来尽我所能: AsyncTask内部的Android AlertDialog 现在,当我尝试向对话框添加属性时,我得到一个NullPointerException: <code>生成器上出现错误。setTitle(“销售工具包

  • 我编写了这段代码,将整个以10为基数的数字转换成二进制。我相信代码就是它所需要的一切,但我无法让工作。 我在这个网站和其他网站上花了几个小时,尝试了无数次的修改,但都没有用。 我已经让代码无错误地编译,但一旦我运行它并输入程序就会崩溃。 下面是代码: 这些是java在我输入数字时抛出的异常。 我希望这是足够的信息。

  • 我正在学习“放心框架”。 我使用http://ziptasticapi.com免费的API为我的演习。 当我打电话: 我得到以下字符串: {“国家”:“美国”、“州”:“正义与发展党”、“城市”:“阿达克”} as响应字符串值。 当我尝试时: 我突然想到: JAVAlang.IllegalStateException:无法分析对象,因为响应中未指定支持的内容类型。内容类型为“文本/html”;字符

  • 问题内容: public class Category { 在正在生成。 问题答案: 当您执行时,您称呼孩子们的。这里没有问题,只不过您在这里调用了父对象。这将称呼孩子,等等。 不错的无限循环。 摆脱它的最好方法是将您的方法更改为: 这样,您将不打印parentCategory,而仅显示其名称,不显示无限循环,不显示StackOverflowError。 编辑: 正如博洛在下面说的那样,您将需要检

  • 请问为什么第13行的错误是未报告的异常,必须捕获pr声明要抛出

  • 我试图实现一个排序和未排序的数组列表。两者都扩展了一个名为AbstractArrayMyList的类,该类包含常见的操作/实现——toString、clear等。。。。 下面是我的AbstractArrayMyList代码(它实现了我定义的通用接口) 我选择对elementData进行保护,以便排序和未排序的专用数组列表可以访问并对其执行操作。这是我对排序数组列表的声明/代码 这一切都很好。然而,