Java compact representation of ECC PublicKey

后端 未结 4 1432
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 08:30

java.security.PublicKey#getEncoded() returns X509 representation of key which in case of ECC adds a lot of overhead compared to raw ECC values.

I\'d lik

4条回答
  •  萌比男神i
    2021-01-06 09:07

    Here is a BouncyCastle approach I've used to unpack the public key:

    public static byte[] extractData(final @NonNull PublicKey publicKey) {
        final SubjectPublicKeyInfo subjectPublicKeyInfo =
                SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
        final byte[] encodedBytes = subjectPublicKeyInfo.getPublicKeyData().getBytes();
        final byte[] publicKeyData = new byte[encodedBytes.length - 1];
    
        System.arraycopy(encodedBytes, 1, publicKeyData, 0, encodedBytes.length - 1);
    
        return publicKeyData;
    }
    

提交回复
热议问题