我们有一个使用Bouncy Castle的应用程序使用PBEWITHSHA256AND128BITAES-CBC-BC算法加密数据.它在运行OpenJDK 1.7的Ubuntu上运行良好.但是当我们将它移动到也运行OpenJDK 1.7的RedHat 6.4时,我们得到以下异常:
java.security.NoSuchAlgorithmException
关于可能导致这种情况的任何想法.我们如何在RedHat 6.4中添加PBEWITHSHA256AND128BITAES-CBC-BC算法?
附:该应用程序在JBoss中运行.
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,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,new EJBException("The algorithm is not found: " + cryptoAlgorithm,e)); } catch (InvalidKeySpecException e) { throw BeanHelper.logException(LOG,new EJBException("The key spec is invalid",e)); }
(在RH 6.4上)
#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)
(在Ubuntu 12.04上)
#java version "1.7.0_15" OpenJDK Runtime Environment (IcedTea7 2.3.7) (7u15-2.3.7-0ubuntu1~12.04) OpenJDK 64-Bit Server VM (build 23.7-b01,mixed mode)
解决方法
你的类路径中是否有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