【发布时间】:2011-04-27 10:28:17
【问题描述】:
我正在尝试在 Java 中以编程方式重现“openssl dhparam -out dh1024.pem 1024”命令的输出。代码 sn -p 如下:-
DHParametersGenerator generator = new DHParametersGenerator();
generator.init(1024, 0, new SecureRandom());
DHParameters params = generator.generateParameters();
// Generator G is set as random in params, but it has to be 2 to conform to openssl
DHParameters realParams = new DHParameters(params.getP(), BigInteger.valueOf(2));
byte[] p = realParams.getP().toByteArray();
byte[] g = realParams.getG().toByteArray();
byte[] l = new byte[(byte) realParams.getL()];
byte[] pgl = new byte[p.length+g.length+l.length];
System.arraycopy(p, 0, pgl, 0, p.length);
System.arraycopy(g, 0, pgl, p.length, g.length);
System.arraycopy(l, 0, pgl, p.length+g.length, l.length);
所以基本上我将 P、G 和 L 参数的值连接到一个字节数组“pgl”中,然后使用 BC 的 PEMWriter 类将其保存在一个文件中。但是当我尝试通过 openssl 使用它时,出现以下错误:-
无法从 /etc/openvpn/easy-rsa/keys/dh1024.pem: 错误:0D07207B:asn1 编码 例程:ASN1_get_object:header 太 长:错误:0D068066:asn1编码 例程:ASN1_CHECK_TLEN:坏对象 标头:错误:0D07803A:asn1 编码 例程:ASN1_ITEM_EX_D2I:嵌套 asn1 错误:错误:0906700D:PEM 例程:PEM_ASN1_read_bio:ASN1 库
.... 这让我相信我错误地编码了 DH 参数,但我找不到任何正确的编码方法。任何人都可以帮助我吗?很多天以来,我的头一直在城堡墙上弹跳,但无济于事....请帮忙:(
【问题讨论】:
-
显然只是将它们连接起来是行不通的,因为任何程序怎么会知道一个参数在哪里结束,下一个参数从哪里开始?
标签: java security bouncycastle cryptoapi