使用cipher.getInstance方法时出现NoSuchAlgorithm异常

时间:2014-12-04 11:02:05

标签: java exception encryption ibm-was

我正在使用密码java进行密码描述逻辑。在那里,使用cipher.getInstance("RSA/NONE/NoPadding")获取密码实例。我得到以下异常:NoSuchAlgorithm

相同的代码在我的本地jboss服务器设置中工作,但不在IBM-WAS服务器设置中工作。本地jboss服务器和WAS服务器之间有什么区别吗?

public static String decrypt(String encrypted, KeyPair keys) {
    Cipher dec;
    try {

        dec = Cipher.getInstance("RSA/NONE/NoPadding"); //Exception raised
        dec.init(Cipher.DECRYPT_MODE, keys.getPrivate());

    } catch (GeneralSecurityException e) {
        throw new RuntimeException("RSA algorithm  not supported", e);//Catch block executed
    }
}

记录:

R Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/NONE/NoPadding
    at java.lang.Throwable.<init>(Throwable.java:80)
    at javax.crypto.Cipher.getInstance(Unknown Source)
    at com.lsi.utils.JCryptionUtil.decrypt(JCryptionUtil.java:59)
    Caused by: java.security.NoSuchAlgorithmException: Mode: NONE not implemented
    at com.ibm.crypto.provider.RSA.engineSetMode(Unknown Source)
    at javax.crypto.Cipher$a_.a(Unknown Source)

**Jar**

Jce.jar - javax.crypto.Cipher;
bcprov-jdk15-140.jar (External security provider jar)

1 个答案:

答案 0 :(得分:3)

Oracle安全提供程序仅支持ECB模式,而不支持NONE。算法由security providers提供,注册到JVM,其名称由提供者创建者提供。

在情况下,如果您事先无法知道,在执行环境中安装了哪些安全提供程序,您可以尝试不同的选项。例如,像这样:

Cipher cipher = null;
try {
    cipher = Cipher.getInstance("RSA/ECB/NoPadding");
} catch (NoSuchAlgorithmException e) {
    cipher = Cipher.getInstance("RSA/NONE/NoPadding");
}

另一种可能性是在启动时检查安装的提供商是否有关算法的决定

for (Provider provider : Security.getProviders()) {
    for (Provider.Service service : provider.getServices()) {
        System.out.println(provider.getName() + ": " + service.getType() + "." + service.getAlgorithm());
        // check these values and find a best match
    }
}