How Do i compress or encode the Elliptic curve public key and put it over the network?

前端 未结 2 2059
栀梦
栀梦 2021-02-15 12:55

I am developing Distributed digital signature that signs a document and send it through network to the Application Server.I am using socket programming in java to do it. I think

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-15 13:23

    I'm pretty sure that the BC implementation uses X9.63 encoding, so these would be rather standardized encodings. You will need to add the Bouncy Castle provider to your JRE (Security.addProvider(new BouncyCastleProvider()), see the bouncy documentation.

    public static void showECKeyEncodings() {
    
        try {
            KeyPairGenerator kp = KeyPairGenerator.getInstance("ECDSA");
            ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable
                    .getParameterSpec("prime192v1");
            kp.initialize(ecSpec);
            KeyPair keyPair = kp.generateKeyPair();
    
            PrivateKey privKey = keyPair.getPrivate();
            byte[] encodedPrivKey = privKey.getEncoded();
            System.out.println(toHex(encodedPrivKey));
    
            PublicKey pubKey = keyPair.getPublic();
            byte[] encodedPubKey = pubKey.getEncoded();
            System.out.println(toHex(encodedPubKey));
    
            KeyFactory kf = KeyFactory.getInstance("ECDSA");
            PublicKey pubKey2 = kf.generatePublic(new X509EncodedKeySpec(encodedPubKey));
            if (Arrays.equals(pubKey2.getEncoded(), encodedPubKey)) {
                System.out.println("That worked for the public key");
            }
    
            PrivateKey privKey2 = kf.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivKey));
            if (Arrays.equals(privKey2.getEncoded(), encodedPrivKey)) {
                System.out.println("That worked for the private key");
            }
    
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }
    
    }
    

提交回复
热议问题