使用“强”字 使用 BouncyCastle 的 JRE 策略文件
密码学新手...我正在尝试使用 BouncyCastle 进行 128 位加密,代码如下。
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.Security;
import java.security.cert.X509Certificate;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Test {
public static void main(String[] args) throws Throwable {
Security.addProvider(new BouncyCastleProvider());
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream keyStoreFile = new FileInputStream("test.p12");
try {
keyStore.load(keyStoreFile, "test12".toCharArray());
} finally {
keyStoreFile.close();
}
X509Certificate certificate = (X509Certificate) keyStore
.getCertificate(keyStore.aliases().nextElement());
OutputStream output = new BufferedOutputStream(new FileOutputStream(
"test.out"));
try {
InputStream input = new FileInputStream("test.in");
try {
CMSEnvelopedDataStreamGenerator generator = new CMSEnvelopedDataStreamGenerator();
generator.addKeyTransRecipient(certificate);
OutputStream encryptedOutput = generator.open(output,
CMSEnvelopedDataGenerator.AES128_WRAP, 128,
BouncyCastleProvider.PROVIDER_NAME);
try {
IOUtils.copy(input, encryptedOutput);
} finally {
encryptedOutput.close();
}
} finally {
input.close();
}
} finally {
output.close();
}
}
}
但我收到此错误:
Exception in thread "main" org.bouncycastle.cms.CMSException: key inappropriate for algorithm.
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at hk.gov.gld.etb.uploading.pkcs7.Test.main(Test.java:45)
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at org.bouncycastle.cms.CMSEnvelopedGenerator$RecipientInf.toRecipientInfo(Unknown Source)
... 4 more
我使用的证书是使用 JDK 的 keytool 程序生成的,如下所示:
keytool -genkeypair -dname "cn=test" -alias test -keystore test.p12 -storepass test12 -validity 180 -storetype pkcs12 -keyalg rsa
我使用的 JDK 版本是 6,我使用的 BouncyCastle 版本是 141。
我这样做的方式正确吗? 我是否还需要安装无限强度策略文件来进行 128 位加密?
非常感谢您的帮助。
谢谢!
Cryptography newbie here... I'm trying to do 128-bit encryption using BouncyCastle with the code below.
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.Security;
import java.security.cert.X509Certificate;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Test {
public static void main(String[] args) throws Throwable {
Security.addProvider(new BouncyCastleProvider());
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream keyStoreFile = new FileInputStream("test.p12");
try {
keyStore.load(keyStoreFile, "test12".toCharArray());
} finally {
keyStoreFile.close();
}
X509Certificate certificate = (X509Certificate) keyStore
.getCertificate(keyStore.aliases().nextElement());
OutputStream output = new BufferedOutputStream(new FileOutputStream(
"test.out"));
try {
InputStream input = new FileInputStream("test.in");
try {
CMSEnvelopedDataStreamGenerator generator = new CMSEnvelopedDataStreamGenerator();
generator.addKeyTransRecipient(certificate);
OutputStream encryptedOutput = generator.open(output,
CMSEnvelopedDataGenerator.AES128_WRAP, 128,
BouncyCastleProvider.PROVIDER_NAME);
try {
IOUtils.copy(input, encryptedOutput);
} finally {
encryptedOutput.close();
}
} finally {
input.close();
}
} finally {
output.close();
}
}
}
But I get this error:
Exception in thread "main" org.bouncycastle.cms.CMSException: key inappropriate for algorithm.
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator.open(Unknown Source)
at hk.gov.gld.etb.uploading.pkcs7.Test.main(Test.java:45)
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at org.bouncycastle.cms.CMSEnvelopedGenerator$RecipientInf.toRecipientInfo(Unknown Source)
... 4 more
The certificate I used was generated using the JDK's keytool program as follows:
keytool -genkeypair -dname "cn=test" -alias test -keystore test.p12 -storepass test12 -validity 180 -storetype pkcs12 -keyalg rsa
The JDK version I use is 6 and the BouncyCastle version I used is 141.
Am I doing it the right way? Do I still need to install the unlimited strength policy files to do 128-bit encryption?
Help is very much appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信是这样 - 你需要 US_export_policy.jar 和 local_policy.jar。 我们需要在我们的项目中做同样的事情,这解决了这个问题。
我认为你生成和使用的密钥应该没问题,否则。 将这两个 jar 添加到 jre/lib/security 中,这应该可以立即修复您的问题。
I believe so - you'll need US_export_policy.jar and local_policy.jar. We needed to do the same thing in our project, and this fixed it up.
I think your keys as generated and used should be fine, otherwise. Add those two jars to jre/lib/security, and that should fix you up straight away.
BouncyCastle 库的版本 141 似乎有一个错误。 当我升级到最新版本(143)时,完全相同的代码可以工作。
Seems that version 141 of the BouncyCastle library has a bug. When I upgraded to the latest version (143), the exact same code worked.