我们有一个使用Bouncy Castle
的应用程序使用PBEWITHSHA256AND128BITAES-CBC-BC
算法加密数据。它在运行Ubuntu
的{{1}}上运行正常。但是,当我们将其移至OpenJDK 1.7
并运行RedHat 6.4
时,我们会遇到以下异常:
java.security.NoSuchAlgorithmException
对可能导致此问题的任何想法。我们如何将OpenJDK 1.7
算法添加到PBEWITHSHA256AND128BITAES-CBC-BC
?
p.s。该应用程序正在RedHat 6.4
中运行。
JBoss
(在RH 6.4 上)
private String cryptoAlgorithm = "PBEWITHSHA256AND128BITAES-CBC-BC";
Security.addProvider(new BouncyCastleProvider());
// load passPhrase from configured external file to char array.
char[] passPhrase = null;
try {
passPhrase = loadPassPhrase(passPhraseFile);
} catch (FileNotFoundException e) {
throw BeanHelper.logException(LOG, methodName, new EJBException("The file not found: " + passPhraseFile, e));
} catch (IOException e) {
throw BeanHelper.logException(LOG, methodName, new EJBException("Error in reading file: " + passPhraseFile, e));
}
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
try {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(cryptoAlgorithm);
SecretKey newSecretKey = secretKeyFactory.generateSecret(pbeKeySpec);
return newSecretKey;
} catch (NoSuchAlgorithmException e) {
throw BeanHelper.logException(LOG, methodName, new EJBException("The algorithm is not found: " + cryptoAlgorithm, e));
} catch (InvalidKeySpecException e) {
throw BeanHelper.logException(LOG, methodName, new EJBException("The key spec is invalid", e));
}
(在Ubuntu 12.04上)
#java -version
java version "1.7.0_19"
OpenJDK Runtime Environment (rhel-2.3.9.1.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
答案 0 :(得分:4)
您的类路径中是否有BouncyCastle提供程序JAR(例如bcprov-jdk15on-149.jar)?
我使用最小的CentOS 6.4(64位)安装,OpenJDK 1.7和BouncyCastle 1.49测试了您的场景,并发现它没有任何问题。
我将JAR放在JRE lib / ext目录中:
/usr/lib/jvm/java-1.7.0-openjdk.x86_64/jre/lib/ext
答案 1 :(得分:2)
我尝试确认您的问题,并在您的环境中看起来像问题。以下是我在干净的OpenJDK 1.7,1.6,Oracle JDK 1.7和1.6
上成功运行的代码示例$ java -version
java version "1.7.0_19"
OpenJDK Runtime Environment (rhel-2.3.9.1.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode):
命令行:java -cp bcprov-jdk15on-149.jar:. Test
输出:OK
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
public class Test {
public static void main(String[] args) throws Exception{
String cryptoAlgorithm = "PBEWITHSHA256AND128BITAES-CBC-BC";
Security.addProvider(new BouncyCastleProvider());
char[] passPhrase = null;
passPhrase = "12321".toCharArray();
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
try {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(cryptoAlgorithm, "BC");
SecretKey newSecretKey = secretKeyFactory.generateSecret(pbeKeySpec);
assert newSecretKey != null;
System.out.println("OK");
} catch (NoSuchAlgorithmException e) {
System.out.println("The algorithm is not found: " + cryptoAlgorithm);
} catch (InvalidKeySpecException e) {
System.out.println("The key spec is invalid");
}
}
}
尝试在您的环境中运行该程序。您可以从这里下载BouncyCastle jar http://downloads.bouncycastle.org/java/bcprov-jdk15on-149.jar
答案 2 :(得分:0)
我猜安全提供商的顺序在两种环境中都是不同的。
for (Provider provider : Security.getProviders())
{
System.out.println("Name: " + provider.getName() + " Version: " + provider.getVersion());
}
您可以尝试将充气城堡提供者插入提供商链中的特定位置。例如,在第一个位置,如果没有使用其他安全提供程序,这不应该导致问题。
Security.insertProviderAt(new BouncyCastleProvider(), 1);
不推荐使用特定的算法提供程序
SecretKeyFactory.getInstance(cryptoAlgorithm, provider)