Java – ECDH + JWE encryption using nimbus-jose and Java 6

ECDH + JWE encryption using nimbus-jose and Java 6… here is a solution to the problem.

ECDH + JWE encryption using nimbus-jose and Java 6

I

have a question and I was wondering if you could help me.

I need to create an encrypted JWE with elliptic curves.

I’m using

jre 1.6.0,nimbus-jose-jwt-8.20-jdk6.jar,bcprov-jdk15to18-166.jar。

I have created a keystore and a key pair using the EC algorithm and the elliptic curve P-512. If I sign a JWT with a private key, then I try to verify it with the public key, everything works fine, but in addition to signing, I need encryption to create a JWE that doesn’t see the payload.

The

following exception is thrown when attempting to encrypt a JWE with a public key

Exception in thread "main" java.lang.NoClassDefFoundError: java/util/Objects
at com.nimbusds.jose.jwk.KeyUse.hashCode(KeyUse.java:121)
at java.util.HashMap.put(Unknown Source)
at com.nimbusds.jose.jwk.KeyUseAndOpsConsistency.<clinit>(KeyUseAndOpsConsistency.java:43)
at com.nimbusds.jose.jwk.JWK.<init>(JWK.java:197)
at com.nimbusds.jose.jwk.ECKey.<init>(ECKey.java:706)
at com.nimbusds.jose.jwk.ECKey$Builder.build(ECKey.java:571)
at com.nimbusds.jose.crypto.ECDHEncrypter.encrypt(ECDHEncrypter.java:217)
at com.nimbusds.jose.JWEObject.encrypt(JWEObject.java:370)
at pruebasJwt.inicioJwt.main(inicioJwt.java:373)

This is the code I used to encrypt :

        //encriptar token
        ECPublicKey publicKey = (ECPublicKey) certificadoBean.getPublicKey();
        Payload payload = new Payload(signedJWT2);
        JWEObject jwe = new JWEObject(jweHeader, payload);                              
        jwe.encrypt(new ECDHEncrypter(publicKey)); **This is where the exception occurs**
        String jweString = jwe.serialize();
        
String tokenJwt = signedJWT2.serialize();
        System.err.println(tokenJwt);

I defined the library in the Eclipe classpath.

Although my requirement is a JWE encrypted with an elliptic curve, I have created a test certificate RSA so that I can generate an encrypted JWE with said certificate without any problems.

I also used the > they put in A very simple example page on https://connect2id.com/products/nimbus-jose-jwt/examples/jws-with-ec-signature and it doesn’t work for me either. I got the same exception when creating the key pair.

public class JweEC {

public static void main(String[] args) {
        System.out.println("############ INICIO JWE FIRMADO CON CERTIFICADO CURVA ELIPTICA ##############");
        System.out.println("soporta ES512" + JCASupport.isSupported(JWSAlgorithm.ES512));
        
Proveedor de criptografica
        Provider bc = BouncyCastleProviderSingleton.getInstance();
        Security.addProvider(bc);
        System.out.println("soporta ES512" + JCASupport.isSupported(JWSAlgorithm.ES512));
        try {
            
ECKey ecJWK = new ECKeyGenerator(Curve.P_521)
                    .generate(); **This is where the exception occurs**
                ECKey ecPublicJWK = ecJWK.toPublicJWK();
        }catch (Exception e) {
             TODO: handle exception
        }

}

}

The NoClassDefFoundError exception indicates that the classloader responsible for dynamic class loading could not find the .class file for the class you are trying to use, but as I said earlier, all my libraries are included in the classpath.

Could it be that I missed some Liberia? I don’t know, I’m lost in this issue

Related Problems and Solutions