/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rsa;

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

/**
 *
 * @author dimitri
 */
public class RSAKeys {
    private int bitLength;
    private BigInteger n;
    private BigInteger phi;

    public RSAKeys(int bitLength) {
        this.bitLength = bitLength;
        BigInteger p = this.randomPrimeNumber();
        BigInteger q = this.randomPrimeNumber();
        this.n = p.multiply(q);

        this.phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));

    }

    private BigInteger randomPrimeNumber() {
        Random prng = new SecureRandom();  // self-seeding
        return BigInteger.probablePrime(this.bitLength, prng);
    }

    public BigInteger[] generatePublicKeys() {
        BigInteger e;

        do {
           e = this.randomPrimeNumber();

        } while(!(e.gcd(this.phi).equals(BigInteger.ONE)));

        BigInteger t[] = {this.n, e};

        return t;       
    }

     public BigInteger generatePrivateKey(BigInteger e) {
        return e.modInverse(this.phi);
    }
}
