PBEWITHSHA256AND128BITAES-CBC-BC在RedHat 6.4上创建java.security.NoSuchAlgorithmException

时间:2021-07-25 18:25:34

We have an application that uses Bouncy Castle to encrypt data using PBEWITHSHA256AND128BITAES-CBC-BC algorithm. It works fine on Unbuntu running OpenJDK 1.7. But when when we move it to RedHat 6.4 also running OpenJDK 1.7, we get the following exception:

我们有一个使用Bouncy Castle的应用程序使用PBEWITHSHA256AND128BITAES-CBC-BC算法加密数据。它在运行OpenJDK 1.7的Unbuntu上运行良好。但是当我们将它移动到也运行OpenJDK 1.7的RedHat 6.4时,我们得到以下异常:

java.security.NoSuchAlgorithmException

Any thoughts on what could be causing this. How can we add PBEWITHSHA256AND128BITAES-CBC-BC algorithm to RedHat 6.4?

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

p.s. the application is running in JBoss.

附:该应用程序在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));
}

(On RH 6.4)

(在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)

(On Ubuntu 12.04)

(在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)

3 个解决方案

#1


3  

Do you have the BouncyCastle provider JAR (e.g. bcprov-jdk15on-149.jar) in your classpath?

你的类路径中是否有BouncyCastle提供程序JAR(例如bcprov-jdk15on-149.jar)?

I tested your scenario with a minimal CentOS 6.4 (64-bit) installation, OpenJDK 1.7 and BouncyCastle 1.49, and found no issues with it.

我使用最小的CentOS 6.4(64位)安装,OpenJDK 1.7和BouncyCastle 1.49测试了您的场景,并发现它没有任何问题。

I placed the JAR in the JRE lib/ext directory:

我将JAR放在JRE lib / ext目录中:

/usr/lib/jvm/java-1.7.0-openjdk.x86_64/jre/lib/ext

#2


2  

I try to confirm your issue and looks like problem in your environment. Here is sample of code i successfully run on clean OpenJDK 1.7, 1.6, Oracle JDK 1.7 and 1.6

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

Command line: java -cp bcprov-jdk15on-149.jar:. Test

命令行:java -cp bcprov-jdk15on-149.jar:。测试

Output: 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");
        }
    }
}

Try to run that program on your environment. BouncyCastle jar you can download from here http://downloads.bouncycastle.org/java/bcprov-jdk15on-149.jar

尝试在您的环境中运行该程序。您可以从这里下载BouncyCastle jar http://downloads.bouncycastle.org/java/bcprov-jdk15on-149.jar

#3


0  



I guess the order of the security providers is different in both environments.

我猜安全提供商的顺序在两种环境中都是不同的。

for (Provider provider : Security.getProviders())
{
    System.out.println("Name: " + provider.getName() + " Version: " + provider.getVersion());
}

you can try to insert the bouncy castle provider at a specific position in the chain of providers. Here for example at the first position, if no other security provider is used this should not lead into problems.

您可以尝试在提供商链中的特定位置插入充气城堡提供商。例如,在第一个位置,如果没有使用其他安全提供者,这不应该导致问题。

Security.insertProviderAt(new BouncyCastleProvider(), 1);

the use of a specific provider for an algorithm is not recommended

不建议使用特定的算法提供程序

SecretKeyFactory.getInstance(cryptoAlgorithm, provider)

see: Java ™ Cryptography Architecture(JCA) Reference Guide

请参阅:Java™加密体系结构(JCA)参考指南

#1


3  

Do you have the BouncyCastle provider JAR (e.g. bcprov-jdk15on-149.jar) in your classpath?

你的类路径中是否有BouncyCastle提供程序JAR(例如bcprov-jdk15on-149.jar)?

I tested your scenario with a minimal CentOS 6.4 (64-bit) installation, OpenJDK 1.7 and BouncyCastle 1.49, and found no issues with it.

我使用最小的CentOS 6.4(64位)安装,OpenJDK 1.7和BouncyCastle 1.49测试了您的场景,并发现它没有任何问题。

I placed the JAR in the JRE lib/ext directory:

我将JAR放在JRE lib / ext目录中:

/usr/lib/jvm/java-1.7.0-openjdk.x86_64/jre/lib/ext

#2


2  

I try to confirm your issue and looks like problem in your environment. Here is sample of code i successfully run on clean OpenJDK 1.7, 1.6, Oracle JDK 1.7 and 1.6

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

Command line: java -cp bcprov-jdk15on-149.jar:. Test

命令行:java -cp bcprov-jdk15on-149.jar:。测试

Output: 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");
        }
    }
}

Try to run that program on your environment. BouncyCastle jar you can download from here http://downloads.bouncycastle.org/java/bcprov-jdk15on-149.jar

尝试在您的环境中运行该程序。您可以从这里下载BouncyCastle jar http://downloads.bouncycastle.org/java/bcprov-jdk15on-149.jar

#3


0  



I guess the order of the security providers is different in both environments.

我猜安全提供商的顺序在两种环境中都是不同的。

for (Provider provider : Security.getProviders())
{
    System.out.println("Name: " + provider.getName() + " Version: " + provider.getVersion());
}

you can try to insert the bouncy castle provider at a specific position in the chain of providers. Here for example at the first position, if no other security provider is used this should not lead into problems.

您可以尝试在提供商链中的特定位置插入充气城堡提供商。例如,在第一个位置,如果没有使用其他安全提供者,这不应该导致问题。

Security.insertProviderAt(new BouncyCastleProvider(), 1);

the use of a specific provider for an algorithm is not recommended

不建议使用特定的算法提供程序

SecretKeyFactory.getInstance(cryptoAlgorithm, provider)

see: Java ™ Cryptography Architecture(JCA) Reference Guide

请参阅:Java™加密体系结构(JCA)参考指南