Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 1.8 Diffie Hellman组14返回InvalidAlgorithmException_Java_Diffie Hellman - Fatal编程技术网

Java 1.8 Diffie Hellman组14返回InvalidAlgorithmException

Java 1.8 Diffie Hellman组14返回InvalidAlgorithmException,java,diffie-hellman,Java,Diffie Hellman,我正在尝试将RFC-3526参数用于Diffie-Hellman组14(2048位密钥),并将默认生成器从RFC设置为2 附件是我正在尝试的代码: KeyPairGenerator hostKeyGen = KeyPairGenerator.getInstance("DH"); DHParameterSpec dhGrp14KeySpec = new DHParameterSpec(new BigInteger(DH_GRP_14_P), BigInteger.valueOf(DH_GRP_14

我正在尝试将RFC-3526参数用于Diffie-Hellman组14(2048位密钥),并将默认生成器从RFC设置为2

附件是我正在尝试的代码:

KeyPairGenerator hostKeyGen = KeyPairGenerator.getInstance("DH");
DHParameterSpec dhGrp14KeySpec = new DHParameterSpec(new BigInteger(DH_GRP_14_P), BigInteger.valueOf(DH_GRP_14_G));
System.out.println("\tP (" + DH_GRP_14_P.length + "): " + dhGrp14KeySpec.getP());
System.out.println("\tG: " + dhGrp14KeySpec.getG());
hostKeyGen.initialize(dhGrp14KeySpec);
System.out.println("Creating Host DH private key...");
KeyPair hostKey = hostKeyGen.generateKeyPair();
System.out.println("Initiating host side Schannel...");
KeyAgreement schannel = KeyAgreement.getInstance("DH");
schannel.init(hostKey.getPrivate());
我得到的错误是:

java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 2048 (inclusive)
    at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120)
    at java.security.KeyPairGenerator$Delegate.initialize(KeyPairGenerator.java:657)
    at java.security.KeyPairGenerator.initialize(KeyPairGenerator.java:399)
    at dhtest.SchannelDHTest.main(SchannelDHTest.java:124)
DH_GRP_14_p字节数组(根据RFC-3526 GRP 14规范):

我数了数字节,它们正好是256字节(2048位)的素数。如何解决这个问题?

新的BigInteger(DH_GRP_14_p)。bitLength()是1982年的。这不是从十六进制数构造大整数的方法

这应该适合您:

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.KeyAgreement;
import javax.crypto.spec.DHParameterSpec;

public class Main {

    public static void main(String[] args) throws Exception {
        KeyPairGenerator hostKeyGen = KeyPairGenerator.getInstance("DH");
        BigInteger p = new BigInteger(modp2048, 16);
        BigInteger g = BigInteger.valueOf(2L);

        System.out.println("p bits: "+p.bitLength());
        System.out.println("g bits: "+g.bitLength());

        DHParameterSpec dhGrp14KeySpec = new DHParameterSpec(p, p);
        hostKeyGen.initialize(dhGrp14KeySpec);
        System.out.println("Creating Host DH private key...");
        KeyPair hostKey = hostKeyGen.generateKeyPair();
        System.out.println("Initiating host side Schannel...");
        KeyAgreement schannel = KeyAgreement.getInstance("DH");
        schannel.init(hostKey.getPrivate());
    }


    private static final String modp2048 = (
            "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" +
            "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" +
            "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" +
            "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" +
            "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" +
            "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" +
            "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" +
            "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" +
            "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" +
            "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" +
            "15728E5A 8AACAA68 FFFFFFFF FFFFFFFF")
            .replaceAll("\\s", "");

}
新的BigInteger(DH_GRP_14_p).bitLength()是1982年推出的。这不是从十六进制数构造大整数的方法

这应该适合您:

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.KeyAgreement;
import javax.crypto.spec.DHParameterSpec;

public class Main {

    public static void main(String[] args) throws Exception {
        KeyPairGenerator hostKeyGen = KeyPairGenerator.getInstance("DH");
        BigInteger p = new BigInteger(modp2048, 16);
        BigInteger g = BigInteger.valueOf(2L);

        System.out.println("p bits: "+p.bitLength());
        System.out.println("g bits: "+g.bitLength());

        DHParameterSpec dhGrp14KeySpec = new DHParameterSpec(p, p);
        hostKeyGen.initialize(dhGrp14KeySpec);
        System.out.println("Creating Host DH private key...");
        KeyPair hostKey = hostKeyGen.generateKeyPair();
        System.out.println("Initiating host side Schannel...");
        KeyAgreement schannel = KeyAgreement.getInstance("DH");
        schannel.init(hostKey.getPrivate());
    }


    private static final String modp2048 = (
            "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" +
            "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" +
            "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" +
            "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" +
            "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" +
            "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" +
            "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" +
            "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" +
            "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" +
            "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" +
            "15728E5A 8AACAA68 FFFFFFFF FFFFFFFF")
            .replaceAll("\\s", "");

}

问题是您没有使用正确的构造函数。设计用于从的输出重构BigInteger。Java使用类似于twos补码方案的方式在字节数组中表示大整数。因此,负的BigInteger由其高阶字节(数组的第0个字节)大于等于128的字节数组表示。正的BigInteger始终由高阶字节<128的字节数组表示。如果正BigInteger的真正高位字节大于等于128,则会发出一个前导为0字节的字节数组

因此,你有两种方法来解决你的问题。您可以在数组前面加上一个零字节,并使用您一直使用的同一个BigInteger构造函数,或者您也可以在字节数组中按原样使用

下面是一个使用字节数组显示差异的示例

import java.math.BigInteger;

public class DhGroup14Toy {

    private static final byte[] DH_GRP_14_P = 
        {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xC9, (byte) 0x0F,
            (byte) 0xDA, (byte) 0xA2, (byte) 0x21, (byte) 0x68, (byte) 0xC2,
            (byte) 0x34, (byte) 0xC4, (byte) 0xC6, (byte) 0x62, (byte) 0x8B,
            (byte) 0x80, (byte) 0xDC, (byte) 0x1C, (byte) 0xD1, (byte) 0x29,
            (byte) 0x02, (byte) 0x4E, (byte) 0x08, (byte) 0x8A, (byte) 0x67,
            (byte) 0xCC, (byte) 0x74, (byte) 0x02, (byte) 0x0B, (byte) 0xBE,
            (byte) 0xA6, (byte) 0x3B, (byte) 0x13, (byte) 0x9B, (byte) 0x22,
            (byte) 0x51, (byte) 0x4A, (byte) 0x08, (byte) 0x79, (byte) 0x8E,
            (byte) 0x34, (byte) 0x04, (byte) 0xDD, (byte) 0xEF, (byte) 0x95,
            (byte) 0x19, (byte) 0xB3, (byte) 0xCD, (byte) 0x3A, (byte) 0x43,
            (byte) 0x1B, (byte) 0x30, (byte) 0x2B, (byte) 0x0A, (byte) 0x6D,
            (byte) 0xF2, (byte) 0x5F, (byte) 0x14, (byte) 0x37, (byte) 0x4F,
            (byte) 0xE1, (byte) 0x35, (byte) 0x6D, (byte) 0x6D, (byte) 0x51,
            (byte) 0xC2, (byte) 0x45, (byte) 0xE4, (byte) 0x85, (byte) 0xB5,
            (byte) 0x76, (byte) 0x62, (byte) 0x5E, (byte) 0x7E, (byte) 0xC6,
            (byte) 0xF4, (byte) 0x4C, (byte) 0x42, (byte) 0xE9, (byte) 0xA6,
            (byte) 0x37, (byte) 0xED, (byte) 0x6B, (byte) 0x0B, (byte) 0xFF,
            (byte) 0x5C, (byte) 0xB6, (byte) 0xF4, (byte) 0x06, (byte) 0xB7,
            (byte) 0xED, (byte) 0xEE, (byte) 0x38, (byte) 0x6B, (byte) 0xFB,
            (byte) 0x5A, (byte) 0x89, (byte) 0x9F, (byte) 0xA5, (byte) 0xAE,
            (byte) 0x9F, (byte) 0x24, (byte) 0x11, (byte) 0x7C, (byte) 0x4B,
            (byte) 0x1F, (byte) 0xE6, (byte) 0x49, (byte) 0x28, (byte) 0x66,
            (byte) 0x51, (byte) 0xEC, (byte) 0xE4, (byte) 0x5B, (byte) 0x3D,
            (byte) 0xC2, (byte) 0x00, (byte) 0x7C, (byte) 0xB8, (byte) 0xA1,
            (byte) 0x63, (byte) 0xBF, (byte) 0x05, (byte) 0x98, (byte) 0xDA,
            (byte) 0x48, (byte) 0x36, (byte) 0x1C, (byte) 0x55, (byte) 0xD3,
            (byte) 0x9A, (byte) 0x69, (byte) 0x16, (byte) 0x3F, (byte) 0xA8,
            (byte) 0xFD, (byte) 0x24, (byte) 0xCF, (byte) 0x5F, (byte) 0x83,
            (byte) 0x65, (byte) 0x5D, (byte) 0x23, (byte) 0xDC, (byte) 0xA3,
            (byte) 0xAD, (byte) 0x96, (byte) 0x1C, (byte) 0x62, (byte) 0xF3,
            (byte) 0x56, (byte) 0x20, (byte) 0x85, (byte) 0x52, (byte) 0xBB,
            (byte) 0x9E, (byte) 0xD5, (byte) 0x29, (byte) 0x07, (byte) 0x70,
            (byte) 0x96, (byte) 0x96, (byte) 0x6D, (byte) 0x67, (byte) 0x0C,
            (byte) 0x35, (byte) 0x4E, (byte) 0x4A, (byte) 0xBC, (byte) 0x98,
            (byte) 0x04, (byte) 0xF1, (byte) 0x74, (byte) 0x6C, (byte) 0x08,
            (byte) 0xCA, (byte) 0x18, (byte) 0x21, (byte) 0x7C, (byte) 0x32,
            (byte) 0x90, (byte) 0x5E, (byte) 0x46, (byte) 0x2E, (byte) 0x36,
            (byte) 0xCE, (byte) 0x3B, (byte) 0xE3, (byte) 0x9E, (byte) 0x77,
            (byte) 0x2C, (byte) 0x18, (byte) 0x0E, (byte) 0x86, (byte) 0x03,
            (byte) 0x9B, (byte) 0x27, (byte) 0x83, (byte) 0xA2, (byte) 0xEC,
            (byte) 0x07, (byte) 0xA2, (byte) 0x8F, (byte) 0xB5, (byte) 0xC5,
            (byte) 0x5D, (byte) 0xF0, (byte) 0x6F, (byte) 0x4C, (byte) 0x52,
            (byte) 0xC9, (byte) 0xDE, (byte) 0x2B, (byte) 0xCB, (byte) 0xF6,
            (byte) 0x95, (byte) 0x58, (byte) 0x17, (byte) 0x18, (byte) 0x39,
            (byte) 0x95, (byte) 0x49, (byte) 0x7C, (byte) 0xEA, (byte) 0x95,
            (byte) 0x6A, (byte) 0xE5, (byte) 0x15, (byte) 0xD2, (byte) 0x26,
            (byte) 0x18, (byte) 0x98, (byte) 0xFA, (byte) 0x05, (byte) 0x10,
            (byte) 0x15, (byte) 0x72, (byte) 0x8E, (byte) 0x5A, (byte) 0x8A,
            (byte) 0xAC, (byte) 0xAA, (byte) 0x68, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF};

    public static void main(String[] args) throws Exception {

        BigInteger bad1 = new BigInteger(DH_GRP_14_P);
        BigInteger good1 = new BigInteger(1, DH_GRP_14_P);
        byte [] augmentedBytes = new byte[DH_GRP_14_P.length + 1];
        System.arraycopy(DH_GRP_14_P, 0, augmentedBytes, 1, DH_GRP_14_P.length);
        BigInteger good2 = new BigInteger(augmentedBytes);
        System.out.println(bad1);
        System.out.println(good1);
        System.out.println(good1.equals(good2));
        System.out.println(good1.isProbablePrime(50));

    }

}
输出为:

-375962762246123711626161428575244919288335676123913432233536670577511994159344116570816223012193827739513685941295892241329166200826129908455464852399221571479961954311415857911811448906479927170863572543208032916411471816282939929662608171512069523761896175670948807549496521472891523674634075413613438850952413260397488155494376493989845498917437341434297119530075724469618920963199914531774956492590825946261921832765452432767164403516209782999137096145451001079860465649681054514172614217905576760949024270284588083855940299806145693407383904354238605196666010342535176157644001104014235140097
32317006071311007300338913926423828248817941241140239112842009751400741706634354222619689417363569347117901737909704191754605873209195028853758986185622153212175412514901774520270235796078236248884246189477587641105928646099411723245426622522193230540919037680524235519125679715870117001058055877651038861847280257976054903569732561526167081339361799541336476559160368317896729073178384589680639671900977202194168647225871031411336429319536193471636533209717077448227988588565369208645296636077250268955505928362751121174096972998068410554359584866583291642136218231078990999448652468262416972035911852507045361090559
true
true

问题是您没有使用正确的构造函数。设计用于从的输出重构BigInteger。Java使用类似于twos补码方案的方式在字节数组中表示大整数。因此,负的BigInteger由其高阶字节(数组的第0个字节)大于等于128的字节数组表示。正的BigInteger始终由高阶字节<128的字节数组表示。如果正BigInteger的真正高位字节大于等于128,则会发出一个前导为0字节的字节数组

因此,你有两种方法来解决你的问题。您可以在数组前面加上一个零字节,并使用您一直使用的同一个BigInteger构造函数,或者您也可以在字节数组中按原样使用

下面是一个使用字节数组显示差异的示例

import java.math.BigInteger;

public class DhGroup14Toy {

    private static final byte[] DH_GRP_14_P = 
        {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xC9, (byte) 0x0F,
            (byte) 0xDA, (byte) 0xA2, (byte) 0x21, (byte) 0x68, (byte) 0xC2,
            (byte) 0x34, (byte) 0xC4, (byte) 0xC6, (byte) 0x62, (byte) 0x8B,
            (byte) 0x80, (byte) 0xDC, (byte) 0x1C, (byte) 0xD1, (byte) 0x29,
            (byte) 0x02, (byte) 0x4E, (byte) 0x08, (byte) 0x8A, (byte) 0x67,
            (byte) 0xCC, (byte) 0x74, (byte) 0x02, (byte) 0x0B, (byte) 0xBE,
            (byte) 0xA6, (byte) 0x3B, (byte) 0x13, (byte) 0x9B, (byte) 0x22,
            (byte) 0x51, (byte) 0x4A, (byte) 0x08, (byte) 0x79, (byte) 0x8E,
            (byte) 0x34, (byte) 0x04, (byte) 0xDD, (byte) 0xEF, (byte) 0x95,
            (byte) 0x19, (byte) 0xB3, (byte) 0xCD, (byte) 0x3A, (byte) 0x43,
            (byte) 0x1B, (byte) 0x30, (byte) 0x2B, (byte) 0x0A, (byte) 0x6D,
            (byte) 0xF2, (byte) 0x5F, (byte) 0x14, (byte) 0x37, (byte) 0x4F,
            (byte) 0xE1, (byte) 0x35, (byte) 0x6D, (byte) 0x6D, (byte) 0x51,
            (byte) 0xC2, (byte) 0x45, (byte) 0xE4, (byte) 0x85, (byte) 0xB5,
            (byte) 0x76, (byte) 0x62, (byte) 0x5E, (byte) 0x7E, (byte) 0xC6,
            (byte) 0xF4, (byte) 0x4C, (byte) 0x42, (byte) 0xE9, (byte) 0xA6,
            (byte) 0x37, (byte) 0xED, (byte) 0x6B, (byte) 0x0B, (byte) 0xFF,
            (byte) 0x5C, (byte) 0xB6, (byte) 0xF4, (byte) 0x06, (byte) 0xB7,
            (byte) 0xED, (byte) 0xEE, (byte) 0x38, (byte) 0x6B, (byte) 0xFB,
            (byte) 0x5A, (byte) 0x89, (byte) 0x9F, (byte) 0xA5, (byte) 0xAE,
            (byte) 0x9F, (byte) 0x24, (byte) 0x11, (byte) 0x7C, (byte) 0x4B,
            (byte) 0x1F, (byte) 0xE6, (byte) 0x49, (byte) 0x28, (byte) 0x66,
            (byte) 0x51, (byte) 0xEC, (byte) 0xE4, (byte) 0x5B, (byte) 0x3D,
            (byte) 0xC2, (byte) 0x00, (byte) 0x7C, (byte) 0xB8, (byte) 0xA1,
            (byte) 0x63, (byte) 0xBF, (byte) 0x05, (byte) 0x98, (byte) 0xDA,
            (byte) 0x48, (byte) 0x36, (byte) 0x1C, (byte) 0x55, (byte) 0xD3,
            (byte) 0x9A, (byte) 0x69, (byte) 0x16, (byte) 0x3F, (byte) 0xA8,
            (byte) 0xFD, (byte) 0x24, (byte) 0xCF, (byte) 0x5F, (byte) 0x83,
            (byte) 0x65, (byte) 0x5D, (byte) 0x23, (byte) 0xDC, (byte) 0xA3,
            (byte) 0xAD, (byte) 0x96, (byte) 0x1C, (byte) 0x62, (byte) 0xF3,
            (byte) 0x56, (byte) 0x20, (byte) 0x85, (byte) 0x52, (byte) 0xBB,
            (byte) 0x9E, (byte) 0xD5, (byte) 0x29, (byte) 0x07, (byte) 0x70,
            (byte) 0x96, (byte) 0x96, (byte) 0x6D, (byte) 0x67, (byte) 0x0C,
            (byte) 0x35, (byte) 0x4E, (byte) 0x4A, (byte) 0xBC, (byte) 0x98,
            (byte) 0x04, (byte) 0xF1, (byte) 0x74, (byte) 0x6C, (byte) 0x08,
            (byte) 0xCA, (byte) 0x18, (byte) 0x21, (byte) 0x7C, (byte) 0x32,
            (byte) 0x90, (byte) 0x5E, (byte) 0x46, (byte) 0x2E, (byte) 0x36,
            (byte) 0xCE, (byte) 0x3B, (byte) 0xE3, (byte) 0x9E, (byte) 0x77,
            (byte) 0x2C, (byte) 0x18, (byte) 0x0E, (byte) 0x86, (byte) 0x03,
            (byte) 0x9B, (byte) 0x27, (byte) 0x83, (byte) 0xA2, (byte) 0xEC,
            (byte) 0x07, (byte) 0xA2, (byte) 0x8F, (byte) 0xB5, (byte) 0xC5,
            (byte) 0x5D, (byte) 0xF0, (byte) 0x6F, (byte) 0x4C, (byte) 0x52,
            (byte) 0xC9, (byte) 0xDE, (byte) 0x2B, (byte) 0xCB, (byte) 0xF6,
            (byte) 0x95, (byte) 0x58, (byte) 0x17, (byte) 0x18, (byte) 0x39,
            (byte) 0x95, (byte) 0x49, (byte) 0x7C, (byte) 0xEA, (byte) 0x95,
            (byte) 0x6A, (byte) 0xE5, (byte) 0x15, (byte) 0xD2, (byte) 0x26,
            (byte) 0x18, (byte) 0x98, (byte) 0xFA, (byte) 0x05, (byte) 0x10,
            (byte) 0x15, (byte) 0x72, (byte) 0x8E, (byte) 0x5A, (byte) 0x8A,
            (byte) 0xAC, (byte) 0xAA, (byte) 0x68, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF};

    public static void main(String[] args) throws Exception {

        BigInteger bad1 = new BigInteger(DH_GRP_14_P);
        BigInteger good1 = new BigInteger(1, DH_GRP_14_P);
        byte [] augmentedBytes = new byte[DH_GRP_14_P.length + 1];
        System.arraycopy(DH_GRP_14_P, 0, augmentedBytes, 1, DH_GRP_14_P.length);
        BigInteger good2 = new BigInteger(augmentedBytes);
        System.out.println(bad1);
        System.out.println(good1);
        System.out.println(good1.equals(good2));
        System.out.println(good1.isProbablePrime(50));

    }

}
输出为:

-375962762246123711626161428575244919288335676123913432233536670577511994159344116570816223012193827739513685941295892241329166200826129908455464852399221571479961954311415857911811448906479927170863572543208032916411471816282939929662608171512069523761896175670948807549496521472891523674634075413613438850952413260397488155494376493989845498917437341434297119530075724469618920963199914531774956492590825946261921832765452432767164403516209782999137096145451001079860465649681054514172614217905576760949024270284588083855940299806145693407383904354238605196666010342535176157644001104014235140097
32317006071311007300338913926423828248817941241140239112842009751400741706634354222619689417363569347117901737909704191754605873209195028853758986185622153212175412514901774520270235796078236248884246189477587641105928646099411723245426622522193230540919037680524235519125679715870117001058055877651038861847280257976054903569732561526167081339361799541336476559160368317896729073178384589680639671900977202194168647225871031411336429319536193471636533209717077448227988588565369208645296636077250268955505928362751121174096972998068410554359584866583291642136218231078990999448652468262416972035911852507045361090559
true
true

由于数组以0xff开头,java认为您的数字是负数。您需要使用。由于数组以0xff开头,java认为您的数字为负数。你需要用电话,谢谢。你们两个都提出了同样好的方法。太糟糕了,我不能选择两个答案都正确。谢谢。你们两个都提出了同样好的方法。太糟糕了,我不能选择两个答案作为正确答案。这应该不是新的DHParameterSpec(p,g)?不应该是新的DHParameterSpec(p,g)