Diffie Hellman密钥交换

时间:2013-06-22 06:45:35

标签: java security diffie-hellman

我目前正在尝试使用java crypto libray进行难度的hellman密钥交换,我已经成功找到了一个安全素数并且还为它生成了一个生成器。但是我似乎无法使用我找到的值创建DH密钥。它给了我以下异常

线程“main”中的异常java.security.InvalidAlgorithmParameterException:Prime大小必须是64的倍数,并且只能是512到1024(含)     在com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120)     at java.security.KeyPairGenerator $ Delegate.initialize(Unknown Source)     at java.security.KeyPairGenerator.initialize(Unknown Source)     在DH.createSpecificKey(DH.java:35)     在DH.main(DH.java:166)

众所周知,在密码学中,我们不能让我们的素数很小。我如何满足我的安全素数和生成器以符合DH库的标准。

以下是我的源代码

public static void createKey()throws Exception
{
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
    kpg.initialize(512);
    KeyPair kp = kpg.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

    DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class);

}

public static void createSpecificKey(BigInteger p,BigInteger g)throws Exception
{
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
    DHParameterSpec param = new DHParameterSpec(p,g);
    kpg.initialize(param);

    KeyPair kp = kpg.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

    DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class);

}


static boolean isPrime(long n)
{
    if (n%2 == 0)
    {
        return false;
    }

    for(int i = 3 ; i*i<=n;i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}


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

    Random randomGenerator = new Random();

    long pValue = randomGenerator.nextInt(1000000);
    long gValue = randomGenerator.nextInt(100000);
    long correctPValue;

    boolean checkPrime = isPrime(pValue);
    System.out.println("the number generated is "+pValue);
    System.out.println(checkPrime);

    while(checkPrime == false)

    {
        long pValue2 = randomGenerator.nextInt(1000000);
        boolean checkPrimeInLoop = isPrime(pValue2);
        //System.out.println("value in loop is "+pValue2);
        if(checkPrimeInLoop == true)
        {
            pValue=pValue2;
            break;
        }
    }


    long checkSP = (pValue*2)+1;
    boolean checkSafePrime = isPrime(checkSP);
    //System.out.println(checkSafePrime);
    while(checkSafePrime==false)
    {
        long pValue3=randomGenerator.nextInt(1000000);
        boolean checkPrimeInLoop = isPrime(pValue3);
        long pValue5=(pValue3*2)+1;
        //boolean checkSafePrimeInLoop = isPrime(pValue4);
        boolean checkSafePrime2InLoop = isPrime(pValue5);

        if(checkSafePrime2InLoop == true && checkPrimeInLoop == true)
        {
            pValue=pValue3;
            break;
        }

    }

    System.out.println("the safe prime is"+pValue);//safe prime

    while(gValue>pValue)
    {
        long gValue2=randomGenerator.nextInt(100000);

        if(gValue2<pValue)
        {
            gValue=gValue2;
            break;
        }
    }

    long getDivisor = (pValue-1)/2;
    BigInteger bi1,bi2,bi3,bi4;

    bi1=BigInteger.valueOf(getDivisor);

    bi2 = BigInteger.valueOf(pValue);

    bi3 = BigInteger.valueOf(gValue);

    bi4= bi3.modPow(bi1,bi2);

    long calculatedValue = bi4.longValue();


    while(calculatedValue == 1)
    {
        long gValue3=randomGenerator.nextInt(100000);
        long getDivisorInLoop = (pValue-1)/2;
        BigInteger bi5,bi6,bi7,bi8;

        bi5=BigInteger.valueOf(getDivisorInLoop);

        bi6 = BigInteger.valueOf(pValue);

        bi7 = BigInteger.valueOf(gValue3);

        bi8= bi7.modPow(bi5,bi6);

        long calculatedValueInLoop = bi8.longValue();
        System.out.println(calculatedValueInLoop);
        if(calculatedValueInLoop!=1)
        {
            gValue=gValue3;
            break;
        }
    }

    BigInteger generatorValue,primeValue;

    generatorValue = BigInteger.valueOf(gValue);
    primeValue = BigInteger.valueOf(pValue);

    createKey();

    int bitLength=512;

    createSpecificKey(generatorValue,primeValue);


}

希望你们能帮助我。提前致谢!

2 个答案:

答案 0 :(得分:2)

你的目标是512位长度: kpg.initialize(512);

您可以像这样生成p和g这样的长度:

int bitLength = 1024;
SecureRandom rnd = new SecureRandom();
BigInteger p = BigInteger.probablePrime(bitLength, rnd);
BigInteger g = BigInteger.probablePrime(bitLength, rnd);

probablePrime可能使用Rabin-Miller或Solovay-Strassen测试,它只得到2 ^ -100的机会(几乎没有机会),得到的整数不是素数。自2002年以来,有一个称为AKS(Agrawal-Kayal-Saxena)的polinomial-time算法,以100%的确定性来测试质数(但到目前为止我还没有看到它使用,可能2 ^ -100对任何人都有好处。)

答案 1 :(得分:0)

我假设您的问题是为什么您只能创建512到1024位的素数,而建议使用2048位?答案很简单:2048位与素数无关,而与模数的大小有关,后者是两个素数的乘积。每个1024位的两个素数将为您提供2048位的模数。这就是为什么DH素数使用1024位是安全的。

关于你的例外:Csaba Toth是对的:你的素数太小,产生它们的方式不是最理想的。只需使用您的createKey()方法即可。