feat: add ecdh demmo
This commit is contained in:
29
src/test/java/me/hatter/tools/tinyencrypt/EcdhDemo.java
Normal file
29
src/test/java/me/hatter/tools/tinyencrypt/EcdhDemo.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package me.hatter.tools.tinyencrypt;
|
||||||
|
|
||||||
|
import me.hatter.tools.commons.bytes.Bytes;
|
||||||
|
import me.hatter.tools.commons.security.ec.ECUtil;
|
||||||
|
import me.hatter.tools.commons.security.key.KeyUtil;
|
||||||
|
|
||||||
|
import javax.crypto.KeyAgreement;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
public class EcdhDemo {
|
||||||
|
// https://stackoverflow.com/questions/21081713/diffie-hellman-key-exchange-in-java
|
||||||
|
// https://git.hatter.ink/hatter/simple-rust-tests/src/branch/master/__crypto/yubikey-rs-demo/src/main.rs
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
final KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH");
|
||||||
|
final byte[] privateKeyBytes = Bytes.fromHex("0101010101010101010101010101010101010101010101010101010101010101").bytes();
|
||||||
|
final PrivateKey privateKey = ECUtil.getECPrivateKey(ECUtil.CURVE_SECP256R1, new BigInteger(privateKeyBytes));
|
||||||
|
keyAgreement.init(privateKey);
|
||||||
|
|
||||||
|
final PublicKey publicKey = KeyUtil.parsePublicKeyPEM("-----BEGIN PUBLIC KEY-----\n" +
|
||||||
|
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3T7r2QbJzwCwjsKfftYYBNHMHRNS\n" +
|
||||||
|
"2SV7YoGR4I/DcXxPrjKYzVxIKc7IvzqUbn22C3hX4Sh/aguuaz8jQvAH0A==\n" +
|
||||||
|
"-----END PUBLIC KEY-----");
|
||||||
|
keyAgreement.doPhase(publicKey, true);
|
||||||
|
System.out.println("e8df486cc3faeabca31bb717b0ae5d038cf2bf3e80569479ccdf69300e0e0cd3");
|
||||||
|
System.out.println(Bytes.from(keyAgreement.generateSecret()).asHex());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user