So this is how I solved my problem :
My keys in strings :
public A : co2D0pNxZJIeQ4RZlCRJYBDzNXSLluETdztid0M+HGzN1uGJ4JWZsenjWgRrmkLh3yqHQqzOBMl/wHVH97A6+g==
private A : TXxii5Ka8LMvuc9arHu63qTmNKxGlgti+wpR3YhBGew=
public B : nUblC+OKdl94iBiWk0941wmYBiMt7C90CjOJPI2BPr8K7xGuC1XsR5DtwFCoM3Iew2BjBG+5SqrYwAPTJF7gdA==
private B : sm6V7+hChvkFSeLNoR+5tItiX8gH5tT47xBkFaV6SDU=
Functions to get private key :
public static PrivateKey getPrivateKey(byte[] encodedPrivateKey) {
BigInteger s = new BigInteger(1,encodedPrivateKey);
ECNamedCurveParameterSpec ecCurve = ECNamedCurveTable.getParameterSpec("secp256r1");
ECParameterSpec ecParameterSpec = new ECNamedCurveSpec("secp256r1", ecCurve.getCurve(), ecCurve.getG(), ecCurve.getN(), ecCurve.getH(), ecCurve.getSeed());
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, ecParameterSpec);
try {
KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
}
}
public static PublicKey rawToEncodedECPublicKey(String curveName, byte[] rawBytes) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
KeyFactory kf = KeyFactory.getInstance("EC");
byte[] x = Arrays.copyOfRange(rawBytes, 0, rawBytes.length/2);
byte[] y = Arrays.copyOfRange(rawBytes, rawBytes.length/2, rawBytes.length);
ECPoint w = new ECPoint(new BigInteger(1,x), new BigInteger(1,y));
return kf.generatePublic(new ECPublicKeySpec(w, ecParameterSpecForCurve(curveName)));
}
public static java.security.spec.ECParameterSpec ecParameterSpecForCurve(String curveName) throws NoSuchAlgorithmException, InvalidParameterSpecException {
AlgorithmParameters params = AlgorithmParameters.getInstance("EC");
params.init(new ECGenParameterSpec(curveName));
return params.getParameterSpec(ECParameterSpec.class);
}
We need to create 2 pairs of keys via keys public/private in string and check if both are equal :
byte [] cle_publique_a_decode = Base64.getDecoder().decode(cle_publique_a);
byte [] cle_privee_a_decode = Base64.getDecoder().decode(cle_privee_a);
byte [] cle_publique_b_decode = Base64.getDecoder().decode(cle_publique_b);
byte [] cle_privee_b_decode = Base64.getDecoder().decode(cle_privee_b);
try {
PublicKey PublicKeyA = rawToEncodedECPublicKey("secp256r1",cle_publique_a_decode);
PublicKey PublicKeyB = rawToEncodedECPublicKey("secp256r1",cle_publique_b_decode);
PrivateKey PrivateKeyA = getPrivateKey(cle_privee_a_decode);
PrivateKey PrivateKeyB = getPrivateKey(cle_privee_b_decode);
// Secret #1
// PrivateKeyA + PublicKeyB = generateSecret
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH");
keyAgreement.init(PrivateKeyA);
keyAgreement.doPhase(PublicKeyB, true);
byte[] generateSecret = keyAgreement.generateSecret();
String base64_generateSecret = Base64.getEncoder().encodeToString(generateSecret);
// Secret #2
// PrivateKeyB + PublicKeyA = generateSecret2
KeyAgreement keyAgreement2 = KeyAgreement.getInstance("ECDH");
keyAgreement.init(PrivateKeyB);
keyAgreement.doPhase(PublicKeyA, true);
byte[] generateSecret2 = keyAgreement.generateSecret();
String base64_generateSecret2 = Base64.getEncoder().encodeToString(generateSecret);
// compare 2 secrets
// (public key A + private key B) == (public key B + private key A)
if(base64_generateSecret.equals(base64_generateSecret2)){
// Good : Secrets are same
// continue..
}
else{
// Not good : Secrets are differents
}
}
catch{
throw new IllegalArgumentException(e.getMessage(), e);
}
Keys are equal, I can begin to encrypt.