Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.math.BigInteger;
import java.security.SecureRandom;

public class Main {
    private static final BigInteger ONE = BigInteger.valueOf(1);

    static BigInteger[] generateSafePrimes(int size, int certainty, SecureRandom random) {
        BigInteger p, q;
        int qLength = size - 1;

        for (;;) {
            q = new BigInteger(qLength, 2, random);

            // p <- 2q + 1
            p = q.shiftLeft(1).add(ONE);

            if (p.isProbablePrime(certainty) && (certainty <= 2 || q.isProbablePrime(certainty))) {
                break;
            }
        }

        return new BigInteger[] { p, q };
    }
}