package ToyRSA;
import java.math.*;
import java.util.Random;

/* 
 * @author Greg M. Bednarski
 * @ver 1.0
 */
public class ToyRSA {
    
    public static void main(String[] args) {
        BigInteger p = new BigInteger("13"); // Here's one prime number..
        BigInteger q = new BigInteger("31"); // ..and another.
        BigInteger n = p.multiply(q);
        BigInteger n2 = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        BigInteger e = generateE(n2);
        BigInteger d = e.modInverse(n2); // Here's the multiplicitave inverse
        
        System.out.println("Encryption keys are: " + e + ", " + n);
        System.out.println("Decryption keys are: " + d + ", " + n);
    }
    

    public static BigInteger generateE(BigInteger fiofn) {
        int y, intGCD;
        BigInteger e;
        BigInteger gcd;
        Random x = new Random();
        // Ok, this is NOT the most effective way to generate 'e'.  Deal with it.
        do {
            y = x.nextInt(fiofn.intValue()-1);
            String z = Integer.toString(y);
            e = new BigInteger(z);
            gcd = fiofn.gcd(e);
            intGCD = gcd.intValue();
           }
        while(y <= 2 || intGCD != 1);
        return e;
    }
}
