0

为了生成 ECDH 密钥对,我在下面编写了与使用 open-ssl 的服务器的代码相同的代码。但是我的 Hex 公钥是这样的(176):

3056301006072a8648ce3d020106052b8104000a0342000462bf4308171cffa3a3ed74a9e72cd3d5fde26d760322a5e39cd67166240f75f87edaffa52bef5e0895a37751f5b90b7b8fc077a90d76893e7cb857c1a49e4818

并且服务器公钥更短,像这样(130):

041f13436e59b8a4e61952b1f184052b330977732f7f8a0505c46028da82bdafd34ded7444a19acdbbf5de91cd914437c3ac9b827dac2f899a4bba961fd72a7ea3

代码:

        KeyPairGenerator kpgen = KeyPairGenerator.getInstance("ECDH", "SC");
        ECGenParameterSpec genspec = new ECGenParameterSpec("secp256k1");
        kpgen.initialize(genspec);

造成这种差异的原因是什么?我使用这一行将公钥转换为字节数组:

kpgen.generateKeyPair().getPublic().getEncoded()).getBytes();

我尝试通过以下方式转换 Android 生成的公钥:

PublicKey pub = kpgen.generateKeyPair().getPublic();    
byte[] pubBytes = pub.getEncoded();
SubjectPublicKeyInfo spkInfo = SubjectPublicKeyInfo.getInstance(pubBytes);
ASN1Primitive primitive = spkInfo.parsePublicKey();
byte[] publicKeyPKCS1 = primitive.getEncoded();

但我得到这个错误:

java.io.EOFException:DEF 长度 126 对象被 63 截断 2021-04-01 17:52:56.113 20381-24882/ 在 org.spongycastle.asn1.DefiniteLengthInputStream.toByteArray(DefiniteLengthInputStream.java:103) 2021-04-01 17 :52:56.113 20381-24882/... W/System.err: 在 org.spongycastle.asn1.ASN1InputStream.createPrimitiveDERObject(ASN1InputStream.java:455) 2021-04-01 17:52:56.113 20381-24882/.. . W/System.err: at org.spongycastle.asn1.ASN1InputStream.buildObject(ASN1InputStream.java:190) 2021-04-01 17:52:56.113 20381-24882/... W/System.err: at org. spongycastle.asn1.ASN1InputStream.readObject(ASN1InputStream.java:278) 2021-04-01 17:52:56.113 20381-24882/... W/System.err: at org.spongycastle.asn1.x509.SubjectPublicKeyInfo.parsePublicKey( SubjectPublicKeyInfo.java:112)

4

0 回答 0