rsarunprashad / crypto

0 stars 0 forks source link

openssl ecparam -name secp256r1-1.2.840.10045.3.1.7 -out #1

Open rsarunprashad opened 5 years ago

rsarunprashad commented 5 years ago

https://neilmadden.blog/2016/05/20/ephemeral-elliptic-curve-diffie-hellman-key-agreement-in-java/

rsarunprashad commented 5 years ago

import java.security.*; import java.security.spec.ECParameterSpec; import java.security.spec.X509EncodedKeySpec; import java.security.interfaces.ECPublicKey;

import javax.crypto.KeyAgreement;

import java.util.*; import java.nio.ByteBuffer; import java.io.Console;

import static javax.xml.bind.DatatypeConverter.printHexBinary; import static javax.xml.bind.DatatypeConverter.parseHexBinary;

public class ecdh {

public static void main(String[] args) throws Exception { Console console = System.console(); // Generate ephemeral ECDH keypair KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC"); kpg.initialize(256); KeyPair kp = kpg.generateKeyPair(); byte[] ourPk = kp.getPublic().getEncoded();

// Display our public key
console.printf("Public Key: %s%n", printHexBinary(ourPk));

// Read other's public key:
byte[] otherPk = parseHexBinary(console.readLine("Other PK: "));

KeyFactory kf = KeyFactory.getInstance("EC");
X509EncodedKeySpec pkSpec = new X509EncodedKeySpec(otherPk);
PublicKey otherPublicKey = kf.generatePublic(pkSpec);

// Perform key agreement
KeyAgreement ka = KeyAgreement.getInstance("ECDH");
ka.init(kp.getPrivate());
ka.doPhase(otherPublicKey, true);

// Read shared secret
byte[] sharedSecret = ka.generateSecret();
console.printf("Shared secret: %s%n", printHexBinary(sharedSecret));

// Derive a key from the shared secret and both public keys
MessageDigest hash = MessageDigest.getInstance("SHA-256");
hash.update(sharedSecret);
// Simple deterministic ordering
List<ByteBuffer> keys = Arrays.asList(ByteBuffer.wrap(ourPk), ByteBuffer.wrap(otherPk));
Collections.sort(keys);
hash.update(keys.get(0));
hash.update(keys.get(1));

byte[] derivedKey = hash.digest();
console.printf("Final key: %s%n", printHexBinary(derivedKey));

} }