diff --git a/src/org/apollo/tools/RsaKeyGenerator.java b/src/org/apollo/tools/RsaKeyGenerator.java new file mode 100644 index 00000000..25d1e392 --- /dev/null +++ b/src/org/apollo/tools/RsaKeyGenerator.java @@ -0,0 +1,46 @@ +package org.apollo.tools; + +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.Random; + +/** + * An RSA key generator. + * + * @author Graham + * @author Major + */ +public final class RsaKeyGenerator { + + /** + * The bit count. Strongly recommended to be at least 2,048. + */ + private static final int BIT_COUNT = 2_048; + + /** + * The entry point of the RsaKeyGenerator. + * + * @param args The application arguments. + */ + public static void main(String[] args) { + Random random = new SecureRandom(); + + BigInteger publicKey = BigInteger.valueOf(65535); + BigInteger p, q, phi, modulus, privateKey; + + do { + p = BigInteger.probablePrime(BIT_COUNT / 2, random); + q = BigInteger.probablePrime(BIT_COUNT / 2, random); + phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); + + modulus = p.multiply(q); + privateKey = publicKey.modInverse(phi); + } while (modulus.bitLength() != BIT_COUNT || privateKey.bitLength() != BIT_COUNT + || !phi.gcd(publicKey).equals(BigInteger.ONE)); + + System.out.println("modulus: " + modulus); + System.out.println("public key: " + publicKey); + System.out.println("private key: " + privateKey); + } + +} \ No newline at end of file