I was trying to understand how the RSA key generation works and therefore tried to implement it in Java. It's generating keys already, but when I'm encrypting and decrypting something using my generated keys it seems like my keys are wrong. This is my code to generate keys:
public void generateNewKeypair(int bit) {
BigInteger[] primes = generatePrimes(2, bit / 2);
BigInteger p = primes[0];
BigInteger q = primes[1];
modulo = q.multiply(p);
BigInteger pN = (q.subtract(BigInteger.valueOf(1))).multiply(p.subtract(BigInteger.valueOf(1)));
publicKey = BigInteger.ZERO;
while (publicKey.equals(BigInteger.ZERO)) {
BigInteger rnd = new BigInteger(pN.bitLength(), random);
if (rnd.compareTo(pN) <= 0 && rnd.gcd(pN).equals(BigInteger.ONE)) {
publicKey = rnd;
}
}
privateKey = publicKey.modInverse(modulo);
}
and thats the entire class: http://pastebin.com/DDQh2Q3n