本文整理汇总了Java中java.math.BigInteger.probablePrime方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.probablePrime方法的具体用法?Java BigInteger.probablePrime怎么用?Java BigInteger.probablePrime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.probablePrime方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: safePrime
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Creates a safe prime number of the given bit length. A prime number <code>p</code> is safe, if p=2*q+1, where q
* is also prime.
*
* @param bitLength the length of the prime number
* @param random the random pool used
* @return java.math.BigInteger which is a safe prime number
*/
public static BigInteger safePrime(int bitLength, SecureRandom random) {
BigInteger p, q;
q = BigInteger.probablePrime(bitLength - 1, random);
p = q.add(q).add(BigInteger.ONE);
while (!p.isProbablePrime(CERTAINTY)) {
do {
q = q.nextProbablePrime();
} while (q.mod(BigInteger.TEN).equals(BigInteger.valueOf(7))
|| !q.remainder(BigInteger.valueOf(4)).equals(BigInteger.valueOf(3)));
p = q.add(q).add(BigInteger.ONE);
while (p.bitLength() != bitLength) {
q = BigInteger.probablePrime(bitLength - 1, random);
p = q.add(q).add(BigInteger.ONE);
}
}
return p;
}
示例2: getPrimeLessThan
import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger getPrimeLessThan(int b, List<BigInteger> banned) throws InternalErrorException {
int bitLength = b;
Random rnd = new Random();
BigInteger probPrime;
int attempt = 0;
do {
probPrime = BigInteger.probablePrime(bitLength, rnd);
attempt++;
} while ((banned.contains(probPrime)) && (attempt < 10));
if (attempt >= 10) {
throw new InternalErrorException("Cannot select moduli.");
}
return probPrime;
}
示例3: testToHexString
import java.math.BigInteger; //导入方法依赖的package包/类
public void testToHexString() throws Exception {
BigInteger expected = BigInteger.probablePrime(256, random());
byte[] bytes = expected.toByteArray();
String hex = MessageDigests.toHexString(bytes);
String zeros = new String(new char[2 * bytes.length]).replace("\0", "0");
String expectedAsString = expected.toString(16);
String expectedHex = zeros.substring(expectedAsString.length()) + expectedAsString;
assertEquals(expectedHex, hex);
BigInteger actual = new BigInteger(hex, 16);
assertEquals(expected, actual);
}
示例4: testHS256
import java.math.BigInteger; //导入方法依赖的package包/类
@Test(groups = TCKConstants.TEST_GROUP_DEBUG,
description = "Validate how to use the HS256 signature alg")
public void testHS256() throws Exception {
JWTClaimsSet claimsSet = JWTClaimsSet.parse("{\"sub\":\"jdoe\"}");
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
SecureRandom random = new SecureRandom();
BigInteger secret = BigInteger.probablePrime(256, random);
JWSSigner signer = new MACSigner(secret.toByteArray());
signedJWT.sign(signer);
}
示例5: generateRandomData
import java.math.BigInteger; //导入方法依赖的package包/类
private static byte[] generateRandomData(){
Random random = new Random();
int bitLength = 512;
log.info("Going to create a random prime number with bit length: " + bitLength);
BigInteger prime = BigInteger.probablePrime(bitLength, random);
return prime.toByteArray();
}
示例6: RSA
import java.math.BigInteger; //导入方法依赖的package包/类
public RSA()
{
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength / 2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}
示例7: createPrimeField
import java.math.BigInteger; //导入方法依赖的package包/类
private PrimeField createPrimeField(SecurityParameters securityParameters) {
log.info("creating prime field");
PrimeField primeField = null;
while (primeField == null) {
BigInteger p_prime = BigInteger.probablePrime(2 * securityParameters.getTau(), secureRandom);
primeField = new PrimeField(p_prime);
}
log.info("prime field created");
return primeField;
}
示例8: openForProject
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Registers the connection and initiates the listening socket.
* @param project the project which is being run / debugged
* @param debugger if true, the connection will pair with a debugger session.
*/
public ShellAgent openForProject(Project p, boolean debugger) throws IOException {
ServerSocket ss;
String encodedKey;
boolean shouldInit = false;
synchronized (this) {
shouldInit = usedKeys.isEmpty();
do {
BigInteger key = BigInteger.probablePrime(64, keyGenerator);
encodedKey = key.toString(Character.MAX_RADIX);
} while (!usedKeys.add(encodedKey));
}
if (shouldInit) {
init();
}
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
SocketAddress local = new InetSocketAddress(
// PENDING: choose something better for remote debugging!
InetAddress.getLoopbackAddress(),
0);
ssc.bind(local);
ssc.accept();
ss = ssc.socket();
LOG.log(Level.FINE, "Creating new server socket {0} for project: {1}", new Object[] {
ss, p
});
ShellAgent agent = new ShellAgent(this, p, ss, encodedKey, debugger);
synchronized (this) {
registeredAgents.put(encodedKey, agent);
}
synchronized (requests) {
servers.wakeup();
requests.add(agent);
}
return agent;
}
示例9: call
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* This method performs the actual selection and return the modulo.
* @return The prime modulo as a BigInteger
* @throws Exception Thrown when selection failed.
*/
@Override
public BigInteger call() throws Exception {
Random rnd = new Random();
return BigInteger.probablePrime(bitLength, rnd);
}
示例10: generateKeyPair
import java.math.BigInteger; //导入方法依赖的package包/类
public KeyPair generateKeyPair() {
// accommodate odd key sizes in case anybody wants to use them
int lp = (keySize + 1) >> 1;
int lq = keySize - lp;
if (random == null) {
random = JCAUtil.getSecureRandom();
}
BigInteger e = publicExponent;
while (true) {
// generate two random primes of size lp/lq
BigInteger p = BigInteger.probablePrime(lp, random);
BigInteger q, n;
do {
q = BigInteger.probablePrime(lq, random);
// convention is for p > q
if (p.compareTo(q) < 0) {
BigInteger tmp = p;
p = q;
q = tmp;
}
// modulus n = p * q
n = p.multiply(q);
// even with correctly sized p and q, there is a chance that
// n will be one bit short. re-generate the smaller prime if so
} while (n.bitLength() < keySize);
// phi = (p - 1) * (q - 1) must be relative prime to e
// otherwise RSA just won't work ;-)
BigInteger p1 = p.subtract(BigInteger.ONE);
BigInteger q1 = q.subtract(BigInteger.ONE);
BigInteger phi = p1.multiply(q1);
// generate new p and q until they work. typically
// the first try will succeed when using F4
if (e.gcd(phi).equals(BigInteger.ONE) == false) {
continue;
}
// private exponent d is the inverse of e mod phi
BigInteger d = e.modInverse(phi);
// 1st prime exponent pe = d mod (p - 1)
BigInteger pe = d.mod(p1);
// 2nd prime exponent qe = d mod (q - 1)
BigInteger qe = d.mod(q1);
// crt coefficient coeff is the inverse of q mod p
BigInteger coeff = q.modInverse(p);
try {
PublicKey publicKey = new RSAPublicKeyImpl(n, e);
PrivateKey privateKey =
new RSAPrivateCrtKeyImpl(n, e, d, p, q, pe, qe, coeff);
return new KeyPair(publicKey, privateKey);
} catch (InvalidKeyException exc) {
// invalid key exception only thrown for keys < 512 bit,
// will not happen here
throw new RuntimeException(exc);
}
}
}
示例11: nextProbablePrime
import java.math.BigInteger; //导入方法依赖的package包/类
public static void nextProbablePrime() throws Exception {
int failCount = 0;
BigInteger p1, p2, p3;
p1 = p2 = p3 = ZERO;
// First test nextProbablePrime on the low range starting at zero
for (int i=0; i<primesTo100.length; i++) {
p1 = p1.nextProbablePrime();
if (p1.longValue() != primesTo100[i]) {
System.err.println("low range primes failed");
System.err.println("p1 is "+p1);
System.err.println("expected "+primesTo100[i]);
failCount++;
}
}
// Test nextProbablePrime on a relatively small, known prime sequence
p1 = BigInteger.valueOf(aPrimeSequence[0]);
for (int i=1; i<aPrimeSequence.length; i++) {
p1 = p1.nextProbablePrime();
if (p1.longValue() != aPrimeSequence[i]) {
System.err.println("prime sequence failed");
failCount++;
}
}
// Next, pick some large primes, use nextProbablePrime to find the
// next one, and make sure there are no primes in between
for (int i=0; i<100; i+=10) {
p1 = BigInteger.probablePrime(50 + i, rnd);
p2 = p1.add(ONE);
p3 = p1.nextProbablePrime();
while(p2.compareTo(p3) < 0) {
if (p2.isProbablePrime(100)){
System.err.println("nextProbablePrime failed");
System.err.println("along range "+p1.toString(16));
System.err.println("to "+p3.toString(16));
failCount++;
break;
}
p2 = p2.add(ONE);
}
}
report("nextProbablePrime", failCount);
}
示例12: nextProbablePrime
import java.math.BigInteger; //导入方法依赖的package包/类
public static void nextProbablePrime() throws Exception {
int failCount = 0;
BigInteger p1, p2, p3;
p1 = p2 = p3 = ZERO;
// First test nextProbablePrime on the low range starting at zero
for (int i=0; i<primesTo100.length; i++) {
p1 = p1.nextProbablePrime();
if (p1.longValue() != primesTo100[i]) {
System.err.println("low range primes failed");
System.err.println("p1 is "+p1);
System.err.println("expected "+primesTo100[i]);
failCount++;
}
}
// Test nextProbablePrime on a relatively small, known prime sequence
p1 = BigInteger.valueOf(aPrimeSequence[0]);
for (int i=1; i<aPrimeSequence.length; i++) {
p1 = p1.nextProbablePrime();
if (p1.longValue() != aPrimeSequence[i]) {
System.err.println("prime sequence failed");
failCount++;
}
}
// Next, pick some large primes, use nextProbablePrime to find the
// next one, and make sure there are no primes in between
for (int i=0; i<100; i+=10) {
p1 = BigInteger.probablePrime(50 + i, random);
p2 = p1.add(ONE);
p3 = p1.nextProbablePrime();
while(p2.compareTo(p3) < 0) {
if (p2.isProbablePrime(100)){
System.err.println("nextProbablePrime failed");
System.err.println("along range "+p1.toString(16));
System.err.println("to "+p3.toString(16));
failCount++;
break;
}
p2 = p2.add(ONE);
}
}
report("nextProbablePrime", failCount);
}
示例13: longPrimeNumber
import java.math.BigInteger; //导入方法依赖的package包/类
private static long longPrimeNumber()
{
BigInteger prime=BigInteger.probablePrime(31,new Random());
return prime.longValue();
}