Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
2
Indexable
Never
import java.security.*;
import java.security.spec.ECGenParameterSpec;

public class GenerateECCKeyPair {

    public static void main(String[] args) throws Exception {
        // Generate ECC key pair
        KeyPair keyPair = generateECCKeyPair();

        // Get the public and private keys
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // Print the keys
        System.out.println("Public Key: " + bytesToHexString(publicKey.getEncoded()));
        System.out.println("Private Key: " + bytesToHexString(privateKey.getEncoded()));
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(String.format("%02X", b));
        }
        return hexString.toString();
    }

    // Generate ECC key pair
    public static KeyPair generateECCKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1"); // You can choose a different curve
        keyGen.initialize(ecSpec, new SecureRandom());
        return keyGen.generateKeyPair();
    }
}
Leave a Comment