如何解决PBEWITHSHA256AND128BITAES-CBC-BC在RedHat 6.4上创建java.security.NoSuchAlgorithmException?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (316)

有一个应用程序用于Bouncy Castle使用PBEWITHSHA256AND128BITAES-CBC-BC算法加密数据。它在Ubuntu运行时工作正常OpenJDK 1.7。但是当我们将它移动到RedHat 6.4同时运行时OpenJDK 1.7,我们得到以下异常:

java.security.NoSuchAlgorithmException

关于可能导致这种情况的任何想法。我们如何添加PBEWITHSHA256AND128BITAES-CBC-BC算法RedHat 6.4

ps应用程序正在运行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, 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));
}

在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
用户回答回答于

我尝试确认问题,并在环境中看起来像问题。以下是我在干净的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");
        }
    }
}

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励