import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;
/**
* Testing prime factor decryption
*
* @author Emmanuel
*/
public class Main {
/**
* Main method
*
* @param args command line arguments
*/
public static void main
(String[] args
) { // private primes and totient
System.
out.
println("Prime 1: " + p1
); System.
out.
println("Prime 2: " + p2
); System.
out.
println("Euler's Totient: " + phi
);
// prime product (modulus) and public exponent
pp = p1.multiply(p2);
//ep = new BigInteger("65537");
ep = coprime(phi, phi.divide(f).add(f));
System.
out.
println("Modulus: " + pp
); System.
out.
println("Public Exponent: " + ep
); System.
out.
println("Private Exponent: " + ep.
modInverse(phi
));
// test encryption
for(int i = 1; i <= 100; i++) {
System.
out.
println(i
+ " => " + c.
modPow(ep, pp
)); }
}
/**
* Calculate the next co-prime of number with minimum defined
*
* @param n the number to check for co-primes
* @param i the minimum to start checking from
* @return the next co-prime or NULL if none is found
*/
for (; i.
compareTo(n
) < 0; i
= i.
add(BigInteger.
ONE)) { return i;
}
}
return null;
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEubWF0aC5CaWdJbnRlZ2VyOwoKLyoqCiAqIFRlc3RpbmcgcHJpbWUgZmFjdG9yIGRlY3J5cHRpb24KICoKICogQGF1dGhvciBFbW1hbnVlbAogKi8KcHVibGljIGNsYXNzIE1haW4gewoKICAgIC8qKgogICAgICogTWFpbiBtZXRob2QKICAgICAqCiAgICAgKiBAcGFyYW0gYXJncyBjb21tYW5kIGxpbmUgYXJndW1lbnRzCiAgICAgKi8KICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICAvLyBwcml2YXRlIHByaW1lcyBhbmQgdG90aWVudAogICAgICAgIEJpZ0ludGVnZXIgcDEsIHAyLCBwaGk7CiAgICAgICAgcDEgPSBuZXcgQmlnSW50ZWdlcigiNjciKTsKICAgICAgICBwMiA9IG5ldyBCaWdJbnR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IHBwKSk7CiAgICAgICAgfQogICAgfQoKICAgIC8qKgogICAgICogQ2FsY3VsYXRlIHRoZSBuZXh0IGNvLXByaW1lIG9mIG51bWJlciB3aXRoIG1pbmltdW0gZGVmaW5lZAogICAgICoKICAgICAqIEBwYXJhbSBuIHRoZSBudW1iZXIgdG8gY2hlY2sgZm9yIGNvLXByaW1lcwogICAgICogQHBhcmFtIGkgdGhlIG1pbmltdW0gdG8gc3RhcnQgY2hlY2tpbmcgZnJvbQogICAgICogQHJldHVybiB0aGUgbmV4dCBjby1wcmltZSBvciBOVUxMIGlmIG5vbmUgaXMgZm91bmQKICAgICAqLwogICAgcHJpdmF0ZSBzdGF0aWMgQmlnSW50ZWdlciBjb3ByaW1lKEJpZ0ludGVnZXIgbiwgQmlnSW50ZWdlciBpKSB7CiAgICAgICAgZm9yICg7IGkuY29tcGFyZVRvKG4pIDwgMDsgaSA9IGkuYWRkKEJpZ0ludGVnZXIuT05FKSkgewogICAgICAgICAgICBpZiAoaS5nY2QobikuZXF1YWxzKEJpZ0ludGVnZXIuT05FKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIGk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIG51bGw7CiAgICB9Cn0K