import java.math.*; import java.util.*; public class Encryption { public static void main(String[] args) { //numbers BigInteger one, firstPrime, secondPrime, key, D, n, P, Q; //inputs Scanner s = new Scanner(System.in); Scanner t = new Scanner(System.in); //prompt to fill a numbers System.out.println("Enter a Prime number for A:"); firstPrime = s.nextBigInteger(); System.out.println("Enter a prime number for B"); secondPrime = s.nextBigInteger(); //creates values for n, firstPrime, and Q to use n = firstPrime.multiply(secondPrime); P = firstPrime.subtract(BigInteger.ONE); Q = secondPrime.subtract(BigInteger.ONE); int x = 0; do { System.out.println("Enter another prime number for the public key"); key = s.nextBigInteger(); //checks to make sure that the numbers you entered were prime if(((firstPrime.gcd(key)).equals(BigInteger.ONE))&& ((Q.gcd(key)).equals(BigInteger.ONE))){ x++;} }while(x==0); //goes until if statement is true, when (i x key).mod((first prime-1) x (second prime - 1)) = 1, the assigns the string rep. of i for(int i = 1;;i++) { D = new BigInteger(String.valueOf(i)); if(((D.multiply(key)).mod(P.multiply(Q))).equals(BigInteger.ONE)) break; } String in = "", out = "", text; System.out.println("Enter plain text to be encrypted"); text = t.nextLine(); for(int i = 0; i < text.length();i++) { //these BIs will serve for our chars being encrypted and decrypted BigInteger encrypt, decrypt; BigInteger transformer = new BigInteger(String.valueOf((int)(text.charAt(i)))); encrypt = transformer.modPow(key, n); out += (char)encrypt.intValue(); decrypt = encrypt.modPow(D, n); in += (char)decrypt.intValue(); System.out.println(" Original letter: " + (char)decrypt.intValue() + "\tEncrypted Letter: " + (char)encrypt.intValue()); } System.out.println("Full text encrypted: " + out); System.out.println("Full text decrypted: " + in); } }