java 导出密钥_如何使用Java 8将Java EC密钥导入/导出到X9.63编码

本文介绍了如何在Java 8环境中使用内置的提供程序,将ECC公钥从X9.63编码导入并导出为PublicKey对象。作者通过测试代码展示了Java生成的ECC公钥的X.509格式,并对比了wolfSSL/wolfCrypt库导出的X9.63编码格式,讨论了两者之间的差异。
摘要由CSDN通过智能技术生成

我正在研究编写一个Java 8程序,以使用只能导出/导入X9.63格式的ECC公钥(它们的wc_ecc_export_x963()函数)的库(wolfSSL / wolfCrypt)与另一个软件进行ECDH交换。我非常希望使用Java附带的提供程序来执行此操作。

因此,我需要找出如何使Java从X9.63编码中创建PublicKey对象,并创建PublicKey对象的X9.63编码字节。

我写了一些测试代码来试图找出Java使用的格式:

// Make a key

ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");

keyGen.initialize(ecSpec);

KeyPair pair = keyGen.generateKeyPair();

// Let's see what Java thinks the encoding is

System.out.println("Pubkey format: " + pair.getPublic().getFormat());

System.out.println("Privkey format: " + pair.getPrivate().getFormat());

// And write out the encoded forms to files so we can poke at them

// with openssl, etc.

try (FileOutputStream pubOut = new FileOutputStream("ecpub.der");

FileOutputStream privOut = new FileOutputStream("ecpriv.der")) {

pubOut.write(pair.getPublic().getEncoded());

privOut.write(pair.getPrivate().getEncoded());

}这产生了输出:

Pubkey format: X.509

Privkey format: PKCS#8然后我可以使用openssl来调查公钥结构:

$ openssl asn1parse -i -in ecpub.der -inform DER

0:d=0 hl=2 l= 89 cons: SEQUENCE

2:d=1 hl=2 l= 19 cons: SEQUENCE

4:d=2 hl=2 l= 7 prim: OBJECT :id-ecPublicKey

13:d=2 hl=2 l= 8 prim: OBJECT :prime256v1

23:d=1 hl=2 l= 66 prim: BIT STRING但对我来说这是不透明的(我不知道ASN.1),我不知道是什么编码格式,甚至不知道它是什么编码格式。

这是一个Java表示的十六进制转储:

$ hexdump -C ecpub.der

00000000 30 59 30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a |0Y0...*.H.=....*|

00000010 86 48 ce 3d 03 01 07 03 42 00 04 a3 c4 5c 5d aa |.H.=....B....\].|

00000020 93 70 8b 65 47 9b f9 83 17 01 37 23 30 d2 0c 6a |.p.eG.....7#0..j|

00000030 c7 93 6e d4 70 b1 5b bf 8e 65 4f 96 70 7e e8 97 |..n.p.[..eO.p~..|

00000040 30 a2 6e e4 1f 50 bb 21 4f a6 7a 01 bd 96 a4 2f |0.n..P.!O.z..../|

00000050 8b cd 0d d0 d2 4a 63 d1 68 d0 7b |.....Jc.h.{|

0000005b

UPDATE

这是我从wc_ecc_export_x963()中获得的hexdump。我也会将C源代码包含到我的测试程序中。文件中的openssl asn1parse扼流圈,FWIW。

$ hexdump -C wolf.x963

00000000 04 f1 55 1b 03 d5 91 ed 03 d5 44 f9 09 b2 1e 59 |..U.......D....Y|

00000010 c7 4d ef 1a e9 de 51 16 4e b9 4d 8c 1d 10 73 d4 |.M....Q.N.M...s.|

00000020 9e 09 24 78 5a 03 c4 45 bf 0c 83 22 69 d8 52 ed |..$xZ..E..."i.R.|

00000030 90 04 00 0c ea 38 95 a9 e5 da 96 d2 ae c4 5c 3a |.....8........\:|

00000040 c8 |.|

00000041作为参考,下面是生成wolf.x963文件的程序(注意 - 我多年没有用C语言编程):

#include

#include

#include

#include

#include

#include

int main() {

// Make a key object ready for use.

ecc_key key;

wc_ecc_init(&key);

// Make a random number generator object ready for use

RNG rng;

wc_InitRng(&rng);

// Make a "32-byte" key. According to wolfSSL,

// this will use the SECP256R1 curve since that's

// what they map to a request for a 32-byte key.

wc_ecc_make_key(&rng, 32, &key);

byte encoded[1024];

word32 encodedLen = 0;

int error;

// According to the API docs, on entry encodedLen should

// be a number equal to or larger than what the output

// will be. If it is not, the function will return BUFFER_E

// and set encodedLen to how many bytes will be needed to

// hold the exported data.

error = wc_ecc_export_x963(&key, encoded, &encodedLen);

printf("Error code = %d\n", error);

if (error == BUFFER_E) {

error = wc_ecc_export_x963(&key, encoded, &encodedLen);

printf("Error code again = %d\n", error);

}

// Print out the byte values so that I can make sure

// that I didn't somehow corrupt the data writing it

// out as I am very rusty at C.

//int i;

//for (i = 0; i < encodedLen; i++) {

// printf("enc[%d] = %x\n", i, encoded[i]);

//}

FILE *outFile;

outFile = fopen("wolf.x963", "wb");

fwrite(encoded, encodedLen, 1, outFile);

fclose(outFile);

wc_ecc_free(&key);

wc_FreeRng(&rng);

return 0;

}其产出:

./SaveEccKey

Error code = -132

Error code again = 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值