我目前正在尝试使用 java 加密库进行 diffie hellman 密钥交换,我已经成功地找到了一个安全的素数以及它的生成器。但是,我似乎无法使用我找到的值创建 DH 密钥。它给了我以下异常
线程“main”中的异常 java.security.InvalidAlgorithmParameterException:素数大小必须是 64 的倍数,并且只能在 com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120) 的范围内为 512 到 1024(含)在 java.security.KeyPairGenerator$Delegate.initialize(Unknown Source) at java.security.KeyPairGenerator.initialize(Unknown Source) at DH.createSpecificKey(DH.java:35) at 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);
}
希望你们能帮助我解决这个问题。提前致谢!