BouncyCastle in Java

14,825

Two problems:

  • The class is named X509EncodedKeySpec not x509EncodedKeySpecs. Fix your import statement.
  • Make sure the BouncyCastle JAR is on the classpath when you compile and run your code.
Share:
14,825
suraj
Author by

suraj

New to the world of TECHNOLOGY

Updated on June 04, 2022

Comments

  • suraj
    suraj almost 2 years

    I have used the following cod in my program to convert byte to public key

    Security.addProvider(new BouncyCastleProvider());
             X509EncodedKeySpec x509keyspec=new X509EncodedKeySpec(b);
    
    
             KeyFactory keyfact=KeyFactory.getInstance("RSA","BC");
             Key pubkey=keyfact.generatePublic(x509keyspec); 
    

    I have imported "import org.bouncycastle.jce.provider.BouncyCastleProvider;",.

    I have added Bouncycastle.jar file into my jre7/lib/ext folder and made changes in javasecurity file by adding following line security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider When i compile code i m getting following error

    Reverse.java:14: error: cannot find symbol
    import java.security.spec.x509EncodedKeySpecs;
                             ^
    symbol:   class x509EncodedKeySpecs
    location: package java.security.spec
    
    Reverse.java:16: error: package org.bouncycastle.jce.provider does not exist
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    
    Reverse.java:49: error: cannot find symbol
    Security.addProvider(new BouncyCastleProvider());
                                      ^
      symbol:   class BouncyCastleProvider
      location: class Reverse
    
  • suraj
    suraj over 12 years
    BouncyCastle.JAR is in c:program files/jre7/lib/ext folder. Still i am getting errors
  • Jesper
    Jesper over 12 years
    Putting it in the lib/ext folder of the JRE doesn't mean that the Java compiler will automatically pick it up when compiling your program. Make sure it is in the classpath when you compile your program.
  • suraj
    suraj over 12 years
    Reverse.java:49: error: cannot find symbol Security.addProvider(new BouncyCastleProvider());
  • suraj
    suraj over 12 years
    In Environment Variable i put a variable called classpath and assigned it path c:program files/jre7/lib/ext/bouncycastle. Still the error is not rectified
  • Jesper
    Jesper over 12 years
    If you literally put c:program files/jre7/lib/ext/bouncycastle in the CLASSPATH, then that's wrong. Add the JAR file to the CLASSPATH correctly. Use the -cp switch on the command line. For example: javac -cp "C:\Program Files\jre7\lib\ext\bouncycastle.jar" Reverse.java
  • suraj
    suraj over 12 years
    C:\Program Files\Java\jdk1.7.0_02\bin>java -cp . Reverse Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/ provider/BouncyCastleProvider at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) at java.lang.Class.getMethod0(Class.java:2685) at java.lang.Class.getMethod(Class.java:1620) at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.jav‌​a:488) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.‌​java:480)
  • suraj
    suraj over 12 years
    I m using jdk 1.7 and i checked out and found out der is no bouncycastle for jdk1.7. Is dat a issue?
  • Jesper
    Jesper over 12 years
    Also put the JAR in the classpath when running the application: java -cp "C:\Program Files\jre7\lib\ext\bouncycastle.jar;." Reverse