1

I am doing password decription logic using cipher java. In that, while getting the instance of cipher using cipher.getInstance("RSA/NONE/NoPadding"). I am getting following exception: NoSuchAlgorithm.

The same code is working in my local jboss server setup but not working in IBM-WAS server setup. Is there any difference between local jboss Server and WAS Server?

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
    }
}

Log:

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)
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168

1 Answers1

3

Oracle security provider supports only ECB mode instead of NONE. Algorithms are provided by security providers, registered to JVM and their names up to provider creators.

In situation, when you cannot know beforehand, which security providers installed in the execution environment, you may try different options. For example, like this:

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

Another possibility is to check installed providers on startup an make decision about algorithms

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
    }
}
divanov
  • 6,173
  • 3
  • 32
  • 51
  • 1
    But note that it is *incorrectly* called ECB as it does not allow for encryption of blocks of data larger than the modulus (key size). Furthermore it is extremely unsafe to use "NoPadding". Use OAEP within a hybrid cryptosystem instead. – Maarten Bodewes Dec 04 '14 at 18:02