0

我试图在 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 使用它时,我收到以下错误:-

无法从 /etc/openvpn/easy-rsa/keys/dh1024.pem 加载 DH 参数:错误:0D07207B:asn1 编码例程:ASN1_get_object:header too long: error:0D068066:asn1 encoding routines:ASN1_CHECK_TLEN:bad object header: error: 0D07803A:asn1 编码例程:ASN1_ITEM_EX_D2I:嵌套 asn1 错误:错误:0906700D:PEM 例程:PEM_ASN1_read_bio:ASN1 lib

....这让我相信我错误地编码了 DH 参数,但我找不到任何正确的编码方法。任何人都可以帮助我吗?很多天以来,我一直把头撞在城堡的墙上,但无济于事....请帮忙:(

4

2 回答 2

4

这是一个例子。请注意,您不能将确定性参数设置为 0,generator.init()否则您将无法获得素数!大部分代码我只是通过查看 Bouncycastle 源代码才知道的,例如查看 PEMWriter 类。

import java.math.BigInteger;
import java.security.SecureRandom;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.crypto.generators.DHParametersGenerator;
import org.bouncycastle.crypto.params.DHParameters;
import org.bouncycastle.util.encoders.Base64;

public class OpenSSLDHParamClone
{

    public static void main(String[] args) throws Exception
    {
        DHParametersGenerator generator = new DHParametersGenerator();
        generator.init(1024, 80, 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));
        ASN1EncodableVector seq = new ASN1EncodableVector();
        seq.add(new DERInteger(realParams.getP()));
        seq.add(new DERInteger(realParams.getG()));
        byte [] derEncoded = new DERSequence(seq).getDEREncoded();
        System.out.println("-----BEGIN DH PARAMETERS-----");
        String b64Encoded = new String(Base64.encode(derEncoded), "US-ASCII");
        while (b64Encoded.length() > 0) {
            int subStringLength = Math.min(64, b64Encoded.length());
            System.out.println(b64Encoded.substring(0, subStringLength));
            b64Encoded = b64Encoded.substring(subStringLength);
        }
        System.out.println("-----END DH PARAMETERS-----");
    }
}
于 2011-04-28T23:46:52.547 回答
3

感谢 GregS,您的解决方案有效,但我最终使用标准 Java 和 BC 的 PemWriter 解决了它,尽管您不能使用这种方法设置生成器 G = 2,但它仍然适用于 openssl 和 Java,这仍然是我最初的目的:)

import java.io.FileWriter;
import java.io.IOException;
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;

public class DHCredentials {

    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, IOException 
        {
        DHCredentials dhc = new DHCredentials();
        System.out.println("This may take a long time ...");

        dhc.saveDHParams("C:\\xxxDH.txt", dhc.genDHParams());

        System.out.println("Done");
    }

    public byte[] genDHParams() throws IOException
    {
        AlgorithmParameterGenerator paramGen = null;
        try 
        {
            paramGen = AlgorithmParameterGenerator.getInstance("DH");
            paramGen.init(1024, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        AlgorithmParameters params = paramGen.generateParameters();
        return params.getEncoded();
    }

    public void saveDHParams(String filePath, byte[] DEREncodedDHParams)
    {
        PemWriter pemWrt;

        try {
            pemWrt = new PemWriter(new FileWriter(filePath));
            pemWrt.writeObject(new PemObject("DH PARAMETERS", DEREncodedDHParams));
            pemWrt.flush();
            pemWrt.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2011-05-07T14:01:02.403 回答