2

I am using following code snippet for getting factory instance of specified algorithm. But it is throwing a java.security.NoSuchAlgorithmException. I am using this in my java project with jre1.6.

Is it require any external library(jar)? The same code when I tried in my Android app, it's working fine.

try {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL");
} catch (Exception e) {
    e.printStackTrace();
}

Provider[] providers = Security.getProviders();
if (null == providers) {
    System.out.println("Providers are not available.");
    return;
}

for (Provider provider : providers) {
    System.out.println("Provider: " + provider.getName());
    Set<Provider.Service> services = provider.getServices();
    for (Provider.Service service : services) {
        System.out.println("\tAlgorithm: " + service.getAlgorithm());
    }
}

try {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(providers[0].getServices().iterator().next().getAlgorithm());
    if (null == factory) {
        System.out.println("Getting instance of specified algorithm failed.");
    } else {
        System.out.println("Success.");
    }
} catch (Exception e) {
    e.printStackTrace();
}

Above is edited code and is throwing following exception:

java.security.NoSuchAlgorithmException: SHA1PRNG SecretKeyFactory not available
    at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
    at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
5
  • Which JCE provider are you using? Commented Oct 17, 2012 at 10:40
  • stack trace of exceptions is as follows: java.security.NoSuchAlgorithmException: PBEWITHMD5AND256BITAES-CBC-OPENSSL SecretKeyFactory not available at javax.crypto.SecretKeyFactory.<init>(DashoA13*..) at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..) Commented Oct 17, 2012 at 10:50
  • I haven't specified any JCE provider. I am using JCE library 6.0 Commented Oct 17, 2012 at 10:51
  • Capitalization? Here docs.oracle.com/javase/6/docs/technotes/guides/security/… they use 'And' and 'With' I also don't see that they have CBC-OPENSSL padding scheme Commented Oct 17, 2012 at 11:13
  • With so few tags (just the java tag at the time of writing), you can be certain it won't be picked up by the crypto community here on stackoverflow. Commented Jan 5, 2013 at 1:28

3 Answers 3

2

"PBEWITHMD5AND256BITAES-CBC-OPENSSL" is part of the Bouncy Castle provider, you may have to download that library to use it on Java SE. Don't forget to download the unlimited strength jurisdiction policy files too.

As for the second exception, if you just ask the algorithm of the first service of the first provider, you get get an algorithm that is only valid for a specific type. In this case it is usable only for SecureRandom, not for SecretKeyFactory as you would have found if you had replaced .getAlgorithm() with .getType().

Sign up to request clarification or add additional context in comments.

Comments

1

According to the documentation for SecretKeyFactory.getInstance(String algorithm):

algorithm - the standard name of the requested secret-key algorithm. See Appendix A in the Java Cryptography Architecture Reference Guide.

and so you should expect and error when calling SecretKeyFactory.getInstance("SHA1PRNG") which isn't valid according to that document.

3 Comments

For "DES" it's working fine, but what about other algorithms? Because I am getting list of providers and algorithms supported by them. What to do in order to use any algorithm supported by the providers which getProviders() method returns?
Here docs.oracle.com/javase/6/docs/api/javax/crypto/…, the documentation for getInstance method directs you to this document for valid algorithm names docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/… .That document contains a bunch of examples which you may find useful. It also contains a reference to this docs.oracle.com/javase/6/docs/technotes/guides/security/…. Search for 'SecretKeyFactory Algorithms'
@DavidSoroko Posted an answer as well, if you are interested. Added cryptography tag, missed this question completely (of course).
-1

Perform the steps here. I did and the result was successful.

Step 1. Download the Bouncy Castle provider for your JDK or JRE (bcprov-jdk16-146.jar for JDK 1.6);

Step 2. Copy the provider .jar file to the Java Runtime (JRE) extensions subfolder; for a Windows machine the JRE 1.6 (if it exists) is installed usually in: example:C:\Program Files\Java\jre6\lib\ext C:\Program Files\Java\jdk1.6.0_16\jre\lib\ext

Step 3. add the Bouncy Castle provider to the java.security file to enable it; the file is located in the \lib\security\ subfolder from the JRE location (Attention ! Both locations – the standalone JRE and the one from the JDK); edit the file and add the statement

security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider Note: N must be the next number in sequence.

Visit --> http://www.itcsolutions.eu/2011/08/22/how-to-use-bouncy-castle-cryptographic-api-in-netbeans-or-eclipse-for-java-jse-projects/

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Welcome to Stack Overflow! In general, answers must be more than just a link. You should edit your answer, to include a summary of the solution described at that link. If not, you can delete your answer and post this as a comment under the question. Link-only answers are subject to deletion by moderators on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.