Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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 DSA(数字签名Alghoritm)实现-密钥生成_Java_Cryptography_Dsa - Fatal编程技术网

Java DSA(数字签名Alghoritm)实现-密钥生成

Java DSA(数字签名Alghoritm)实现-密钥生成,java,cryptography,dsa,Java,Cryptography,Dsa,我必须为大学实施DSA,我在查找数字q时遇到问题,这是p-1的素数因子,其中p是素数。我试图编写一些wierd循环,但它只适用于较小的p值。对于512位长的素数,我想需要很多时间。 我使用Java和BigInteger库实现 编辑: 我不确定你在做什么,但这段代码说明了我的评论。它通常在我的笔记本电脑上运行不到0.5秒 import java.math.BigInteger; import java.security.SecureRandom; public class Main {

我必须为大学实施DSA,我在查找数字q时遇到问题,这是p-1的素数因子,其中p是素数。我试图编写一些wierd循环,但它只适用于较小的p值。对于512位长的素数,我想需要很多时间。 我使用Java和BigInteger库实现

编辑:


我不确定你在做什么,但这段代码说明了我的评论。它通常在我的笔记本电脑上运行不到0.5秒

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

public class Main {

    public static BigInteger[] generatePAndQ() {
        SecureRandom random = new SecureRandom();

        final int pSizeInBits = 512;
        final int qSizeInBits = 160;
        BigInteger q = BigInteger.probablePrime(qSizeInBits, random);
        BigInteger k = BigInteger.ONE.shiftLeft(pSizeInBits - qSizeInBits); // k = 2**(pSizeInBits - qSizeInBits);

        BigInteger probablyPrime = q.multiply(k).add(BigInteger.ONE); // probablyPrime = q * k + 1
        while (!probablyPrime.isProbablePrime(50)) {
            q = BigInteger.probablePrime(qSizeInBits, random);
            probablyPrime = q.multiply(k).add(BigInteger.ONE);
        }

        BigInteger[] qAndP = new BigInteger[2];
        qAndP[0] = q;
        qAndP[1] = probablyPrime;

        return qAndP;
    }

    public static void main(String[] args) {
        long start = System.nanoTime();
        final BigInteger[] pAndQ = generatePAndQ();
        double elapsed = (System.nanoTime() - start) / 1e9;
        System.out.printf("q=%d%np=%d%nTime: %f (seconds)%n", pAndQ[0], pAndQ[1], elapsed);
    }
}

q、p和k的边界既快又脏,应该清理干净。

这可能有助于做到这一点的一种方法是从q开始。首先找到一个大小合适的素数q,然后检查p=2*q+1的值是否为素数。我相信预期的运行时间是O(log^2p)。如果你想要调试帮助,你真的需要显示你的代码@詹姆斯波尔克:你做过类似的事情吗?嗯,它可以工作,但仍然很慢。对于30位q,搜索大约5分钟,我遇到了160位One,在循环中不创建新的随机实例。相反,在循环外创建一个单一的SecureRandom实例,并将该实例用于所有random。。。。这是因为函数“isPrime(probablyPrime)”。。。。无论如何,非常感谢你。我在找如何计算q和p两天。再次感谢你的帮助!
import java.math.BigInteger;
import java.security.SecureRandom;

public class Main {

    public static BigInteger[] generatePAndQ() {
        SecureRandom random = new SecureRandom();

        final int pSizeInBits = 512;
        final int qSizeInBits = 160;
        BigInteger q = BigInteger.probablePrime(qSizeInBits, random);
        BigInteger k = BigInteger.ONE.shiftLeft(pSizeInBits - qSizeInBits); // k = 2**(pSizeInBits - qSizeInBits);

        BigInteger probablyPrime = q.multiply(k).add(BigInteger.ONE); // probablyPrime = q * k + 1
        while (!probablyPrime.isProbablePrime(50)) {
            q = BigInteger.probablePrime(qSizeInBits, random);
            probablyPrime = q.multiply(k).add(BigInteger.ONE);
        }

        BigInteger[] qAndP = new BigInteger[2];
        qAndP[0] = q;
        qAndP[1] = probablyPrime;

        return qAndP;
    }

    public static void main(String[] args) {
        long start = System.nanoTime();
        final BigInteger[] pAndQ = generatePAndQ();
        double elapsed = (System.nanoTime() - start) / 1e9;
        System.out.printf("q=%d%np=%d%nTime: %f (seconds)%n", pAndQ[0], pAndQ[1], elapsed);
    }
}