Example usage for java.math BigInteger probablePrime

List of usage examples for java.math BigInteger probablePrime

Introduction

In this page you can find the example usage for java.math BigInteger probablePrime.

Prototype

public static BigInteger probablePrime(int bitLength, Random rnd) 

Source Link

Document

Returns a positive BigInteger that is probably prime, with the specified bitLength.

Usage

From source file:Main.java

public static void main(String[] args) {

    int bitLength = 3;

    // create a random object
    Random rnd = new Random();

    BigInteger bi = BigInteger.probablePrime(bitLength, rnd);

    System.out.println(bi);//from w  w  w. j  a v a 2 s  .co  m
}

From source file:MainClass.java

public static void main(String[] unused) {
    Random prng = new SecureRandom(); // self-seeding
    System.out.println(BigInteger.probablePrime(10, prng));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);

    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);//  ww w  .j a  v a 2s. c  o m
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    BigInteger p = new BigInteger(Integer.toString(pValue));
    BigInteger g = new BigInteger(Integer.toString(gValue));
    BigInteger Xa = new BigInteger(Integer.toString(XaValue));
    BigInteger Xb = new BigInteger(Integer.toString(XbValue));

    createKey();/*from  ww  w  .  java  2s.co  m*/

    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    p = BigInteger.probablePrime(bitLength, rnd);
    g = BigInteger.probablePrime(bitLength, rnd);

    createSpecificKey(p, g);
}

From source file:Main.java

public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits) {
    int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1;
    Random random = new SecureRandom();
    BigInteger ret = BigInteger.probablePrime(numbits, random);
    return ret;/*from  w  ww.  j  ava2s .  co  m*/
}

From source file:Main.java

public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits, Random random) {
    int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1;

    BigInteger ret = BigInteger.probablePrime(numbits, random);
    return ret;//from w  ww. ja  va 2 s .  com
}

From source file:com.og.elliptic.sdk.main.java

private static void testDiffieHelman(WeierStrassCurve curve) {
    PointGMP generateur = new PointGMP(curve.getGx(), curve.getGy(), curve);// le
    // point/*from  w  ww .  jav  a 2s  .  com*/
    // gnrateur
    // qui
    // servira
    // calculer
    // notre
    // clef
    // secrte
    Random rnd = new Random();

    // calcul chez Alice
    BigInteger clefSecreteAlice = BigInteger.probablePrime(7, rnd);// la
    // clef
    // secrte
    // n'appartient
    // qu'
    // Alice
    PointGMP pA = generateur.mult(clefSecreteAlice);// pA est la clef
    // publique que l'on va
    // partager  Bob
    if (pA.isOnCurve()) {
        System.out.println("On Curve");
    } else {
        System.out.println("Not on curve");
    }

    // calcul chez Bob
    BigInteger clefSecreteBob = BigInteger.probablePrime(7, rnd);// la clef
    // secrte
    // n'appartient
    // qu'
    // Bob
    PointGMP pB = generateur.mult(clefSecreteBob);// pB est la clef publique
    // que l'on va partager
    //  alice

    // on partage donc en clair pB et pA (pas scuris mais pas de de
    // serveur dans ce cas l)
    // calcul chez Alice
    BigInteger kA = BigInteger.probablePrime(7, rnd);// un chiffre random
    // pour Alice
    PointGMP middleA = generateur.mult(kA);// middleA est la clef
    // intermdiaire d'Alice

    // Calcul chez Bob
    BigInteger kB = BigInteger.probablePrime(7, rnd);// un chiffre random
    // pour Bob
    PointGMP middleB = generateur.mult(kB);// middleB est la clef
    // intermdiaire de Bob

    // tout est vrifi chacun calcule leur clef partage finale
    PointGMP finBob = middleA.mult(kB);
    PointGMP finAlice = middleB.mult(kA);

    if (finBob.equal(finAlice))
        System.out.println("Diffie Hellman STS complet");
    else
        System.out.println("Probleme Diffie Hellman STS");

    System.out.println("fin Bob " + finBob.toString());
    System.out.println("fin Ali " + finAlice.toString());
}

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

/**
 * Creates a new self-signed X509 certificate
 *
 * @param pair the public/private keypair- the pubkey will be added to the cert and the private
 * key will be used to sign the certificate
 * @param subject the distinguished name of the subject
 * @param isAuthority true to make the cert a CA cert, false otherwise
 * @return//from ww  w. j a v a  2s  .  c  om
 */
public static X509Certificate newSelfSignedCertificate(KeyPair pair, X500Name subject, boolean isAuthority) {
    X509v3CertificateBuilder b = new JcaX509v3CertificateBuilder(subject,
            BigInteger.probablePrime(128, new SecureRandom()), Date.from(Instant.now().minusSeconds(1)),
            Date.from(LocalDateTime.now().plusYears(3).toInstant(ZoneOffset.UTC)), subject, pair.getPublic());
    try {
        b.addExtension(Extension.basicConstraints, true, new BasicConstraints(isAuthority));
    } catch (CertIOException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        X509CertificateHolder bcCert = b.build(
                new JcaContentSignerBuilder(SIGNING_ALGORITHM).setProvider("BC").build(pair.getPrivate()));
        return new JcaX509CertificateConverter().setProvider("BC").getCertificate(bcCert);
    } catch (CertificateException | OperatorCreationException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.og.elliptic.sdk.main.java

private static void testDiffieHelmanAndSign(WeierStrassCurve curve) {
    PointGMP generateur = new PointGMP(curve.getGx(), curve.getGy(), curve);
    Random rnd = new Random();

    // calcul chez Alice
    BigInteger clefSecreteAlice = BigInteger.probablePrime(7, rnd);
    PointGMP pA = generateur.mult(clefSecreteAlice);
    if (pA.isOnCurve()) {
        System.out.println("On Curve");
    } else {//w  ww  . j  a va 2  s . com
        System.out.println("Not on curve");
    }

    // on partage donc en clair pB et pA
    // calcul chez Alice
    BigInteger kA = BigInteger.probablePrime(7, rnd);// un chiffre random
    // pour Alice
    PointGMP middleA = generateur.mult(kA);// middleA est la clef
    // intermdiaire d'Alice

    MessageDigest digest = null;
    try {
        digest = MessageDigest.getInstance("SHA-256");
        // calcul chez Alice
        BigInteger hashA = new BigInteger(1, digest.digest(middleA.toString().getBytes("UTF-8")));

        // calcul chez Alice
        PointGMP signatureA = curve.sign_ECDSA(hashA, clefSecreteAlice);
        if (signatureA.isOnCurve()) {
            System.out.println("On Curve");
        } else {
            System.out.println("Not on curve");
        }
        // on envoie donc middleA et signature A  Bob

        // calcul chez bob ==> on vrifie la signature avant de continuer
        boolean resA = curve.verif_ECDSA(signatureA.getX(), signatureA.getY(), pA, hashA);
        if (resA)
            System.out.println("Signature ok d'Alice");
        else
            System.out.println("Probleme de signature d'Alice");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

/**
 * Given a certificate signing request, produce a signed certificate.
 *
 * @param caKey//ww  w .j a  v a  2 s .c o  m
 * @param caCert
 * @param r
 * @param makeAuthority
 * @return
 */
public static X509Certificate fulfillCertRequest(PrivateKey caKey, X509Certificate caCert,
        PKCS10CertificationRequest r, boolean makeAuthority) {
    X509v3CertificateBuilder b = new JcaX509v3CertificateBuilder(new X500Name(caCert.getSubjectDN().getName()), // the order of O,OU,CN returned is very important
            BigInteger.probablePrime(128, new SecureRandom()), Date.from(Instant.now().minusSeconds(1)),
            Date.from(LocalDateTime.now().plusYears(3).toInstant(ZoneOffset.UTC)), r.getSubject(),
            getPublicKeyFromInfo(r.getSubjectPublicKeyInfo()));

    try {
        b.addExtension(Extension.basicConstraints, true, new BasicConstraints(makeAuthority));
    } catch (CertIOException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        ContentSigner signer = new JcaContentSignerBuilder(SIGNING_ALGORITHM).setProvider("BC").build(caKey);
        X509CertificateHolder build = b.build(signer);
        return new JcaX509CertificateConverter().setProvider("BC").getCertificate(build);
    } catch (OperatorCreationException | CertificateException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}