【发布时间】:2021-09-20 01:59:22
【问题描述】:
我正在尝试解密文件“test.txt.p7b”,该文件使用 JKS 中的证书加密。
我在调试代码时收到此错误。感谢有人可以解释为什么会出现此错误。是我的钥匙有问题还是我的代码有问题(大多数情况下,我相信是这样)。非常感谢
错误信息如下,
Exception in thread "main" org.bouncycastle.cms.CMSException: exception unwrapping key: bad padding: Decryption error
at org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.KeyTransRecipientInformation.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContentStream(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContent(Unknown Source)
at TestingB.decryptData(TestingB.java:299)
at TestingB.main(TestingB.java:161)
Caused by: org.bouncycastle.operator.OperatorException: bad padding: Decryption error
at org.bouncycastle.operator.jcajce.JceAsymmetricKeyUnwrapper.generateUnwrappedKey(Unknown Source)
... 7 more
Caused by: javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
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)
... 8 more
这是我的解密代码。
FileInputStream fIn = new FileInputStream(_keyStorePath);
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(fIn, _password);
PrivateKey key = (PrivateKey) keystore.getKey("def","123456".toCharArray());
fIn.close();
File file = new File("C:\\1_Eclipse\\1_CS\\Encrypted\\test.txt.p7b");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] encryptedAndSignedByte = new byte[(int)file.length()];
fileInputStream.read(encryptedAndSignedByte);
fileInputStream.close();
X509Certificate cert9 = (X509Certificate) keystore.getCertificate("abc");
KeyTransRecipientId recId = new JceKeyTransRecipientId(cert9.getIssuerX500Principal(), cert9.getSerialNumber());
CMSEnvelopedData enveloped = new CMSEnvelopedData(encryptedAndSignedByte);
RecipientInformationStore recipients = enveloped.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
JceKeyTransEnvelopedRecipient ter = new JceKeyTransEnvelopedRecipient(key);
ter.setContentProvider(BouncyCastleProvider.PROVIDER_NAME);
System.out.println("content : " + recipient.getContent(ter));
【问题讨论】:
标签: java encryption jks