Bouncycastle java compilation error

This is my first experience with certificates and I am completely new to this. I had a compile-time error and I couldn't find anything on the internet about it, nothing with the same error was found. I have been struggling with it since 4 days, there was nothing I could do :( I would really appreciate your help. Thanks.

here is my code: main class

package tn.ance.signature;
import java.security.*;
import java.security.cert.X509Certificate;
public class main_project {
    public static void main(String[] args) {
        chargement_fichier cf = new chargement_fichier();
        KeyStore ks = cf.charger_fichier();
        char[] password = cf.getPassword();
        // RECUPERATION DU COUPLE CLE PRIVEE/PUBLIQUE ET DU CERTIFICAT PUBLIQUE
        X509Certificate cert = null;
        PrivateKey privatekey = null;
        PublicKey publickey = null;

        recuperation_cles_cert recup =new recuperation_cles_cert(); 
        privatekey = recup.getPrivatekey();
        publickey = recup.getPublickey();
        cert = recup.getCert();

        System.out.println("Private Key Format:");
        System.out.println(privatekey.getFormat());
        System.out.println("Private Key Algorithme:");
        System.out.println(privatekey.getAlgorithm());
        System.out.println("Public Key Format:");
        System.out.println(publickey.getFormat());
        System.out.println("Public Key Algorithme:");
        System.out.println(publickey.getAlgorithm());
        System.out.println("Certification:");
        System.out.println("SubjectDN:");
        System.out.println(cert.getSubjectDN());
        System.out.println("Not After:");
        System.out.println(cert.getNotAfter());
        System.out.println("Not Before:");
        System.out.println(cert.getNotBefore());

    }
}

      

**

file upload class

package tn.ance.signature;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class chargement_fichier {
     private char[] password = null;
    // CHARGEMENT DU FICHIER PKCS#12
     public KeyStore charger_fichier(){
            KeyStore ks = null;
            Security.addProvider(new BouncyCastleProvider());
            try {
                ks = KeyStore.getInstance("PKCS12");
                // Password pour le fichier personnal_nyal.p12
                password = "123456".toCharArray();
                FileInputStream fl = new FileInputStream("T:/Stage/150722/syrine.p12");
                ks.load(fl, password);
                return  ks;
            } catch (Exception e) {
                System.out.println("Erreur: fichier " +
                                   "syrine.p12" +
                                   " n'est pas un fichier pkcs#12 valide ou passphrase incorrect");
                return null ;
            }
         }
      public char[] getPassword(){
             return password;
             }
}

      

downloading keys and certificates

package tn.ance.signature;
    import java.security.KeyStore;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.cert.X509Certificate;
    import java.util.Enumeration;
    import java.util.Vector;
    public class recuperation_cles_cert {
        private X509Certificate cert = null;
        private PrivateKey privatekey = null;
        private PublicKey publickey = null;
        private KeyStore ks;
        private char[] password;

    public PrivateKey getPrivatekey(){

        chargement_fichier cf = new chargement_fichier(); 
        ks = cf.charger_fichier();
        password = cf.getPassword();

        try {
            Enumeration en = ks.aliases();
            String ALIAS = "";
            Vector vectaliases = new Vector();
            while (en.hasMoreElements())
                vectaliases.add(en.nextElement());
            String[] aliases = (String []) (vectaliases.toArray(new String[0]));
            for (int i = 0; i < aliases.length; i++)
                if (ks.isKeyEntry(aliases[i]))
                {
                    ALIAS = aliases[i];
                    break;
                }

            privatekey = (PrivateKey)ks.getKey(ALIAS, password);
            return privatekey;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        }



    public PublicKey getPublickey(){

        chargement_fichier cf = new chargement_fichier(); 
        ks = cf.charger_fichier();
        password = cf.getPassword();

        try {
            Enumeration en = ks.aliases();
            String ALIAS = "";
            Vector vectaliases = new Vector();
            while (en.hasMoreElements())
                vectaliases.add(en.nextElement());
            String[] aliases = (String []) (vectaliases.toArray(new String[0]));
            for (int i = 0; i < aliases.length; i++)
                if (ks.isKeyEntry(aliases[i]))
                {
                    ALIAS = aliases[i];
                    break;
                }

            publickey = ks.getCertificate(ALIAS).getPublicKey();
            return publickey;
        } catch (Exception e) {
            e.printStackTrace();
            return null ;
        }
         }

    public X509Certificate getCert(){

        chargement_fichier cf = new chargement_fichier(); 
        ks = cf.charger_fichier();
        password = cf.getPassword();

        try {
            Enumeration en = ks.aliases();
            String ALIAS = "";
            Vector vectaliases = new Vector();
            while (en.hasMoreElements())
                vectaliases.add(en.nextElement());
            String[] aliases = (String []) (vectaliases.toArray(new String[0]));
            for (int i = 0; i < aliases.length; i++)
                if (ks.isKeyEntry(aliases[i]))
                {
                    ALIAS = aliases[i];
                    break;
                }
            cert = (X509Certificate)ks.getCertificate(ALIAS);
            return cert;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        }

}

      

**

and here is the error

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/cryptopro/CryptoProObjectIdentifiers
    at org.bouncycastle.jcajce.provider.digest.GOST3411$Mappings.configure(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.loadAlgorithms(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.setup(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.access$000(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.<init>(Unknown Source)
    at tn.ance.signature.chargement_fichier.charger_fichier(chargement_fichier.java:16)
    at tn.ance.signature.main_project.main(main_project.java:13)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.

loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 9 more

      

**

Update I added bcprov-jdk16-1.45 to the C: \ Program Files \ Java \ jdk1.7.0_75 \ lib folder and added it to the external jars path in the project, but now I get this error

Exception in thread "main" java.lang.NoSuchFieldError: gostR3411
    at org.bouncycastle.jcajce.provider.digest.GOST3411$Mappings.configure(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.loadAlgorithms(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.setup(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider.access$000(Unknown Source)
    at org.bouncycastle.jce.provider.BouncyCastleProvider$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at org.bouncycastle.jce.provider.BouncyCastleProvider.<init>(Unknown Source)
at tn.ance.signature.chargement_fichier.charger_fichier(chargement_fichier.java:15)
at tn.ance.signature.main_project.main(main_project.java:13)

      

+3


source to share


1 answer


Try using jdk15on-152 - release 1.45 ends. The jar options with jdk15on on their behalf are for JDK 1.5 for JDK 1.8 (we tried using jdk15 + first, but it turned out that maven couldn't handle +).



+2


source







All Articles