如何在Java中使用BouncyCastle正确编码DH参数?
|
我试图在Java中以编程方式重现\“ openssl dhparam -out dh1024.pem 1024 \”命令的输出。该代码段如下:-
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使用它时,出现以下错误:-
无法从中加载DH参数
/etc/openvpn/easy-rsa/keys/dh1024.pem:
错误:0D07207B:asn1编码
例程:ASN1_get_object:标头
long:错误:0D068066:asn1编码
例程:ASN1_CHECK_TLEN:坏对象
标头:错误:0D07803A:asn1编码
例程:ASN1_ITEM_EX_D2I:嵌套asn1
错误:错误:0906700D:PEM
例程:PEM_ASN1_read_bio:ASN1 lib
....这使我相信我对DH参数的编码错误,但是我找不到任何正确的编码方式。有人可以帮我吗?我已经将头弹到城堡的墙壁上很多天了,但无济于事....请帮助:(
没有找到相关结果
已邀请:
2 个回复
邵酮
中将确定性参数设置为0,否则不会得到质数!我仅通过查看Bouncycastle源代码(例如,查看PEMWriter类)就知道了大部分代码。
授巨