package uk.ac.cam.gpe21.droidssl.mitm.cert;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;

import java.math.BigInteger;
import java.security.SecureRandom;

public final class KeyPairGenerator {
	private static final BigInteger PUBLIC_EXPONENT = new BigInteger("10001", 16); /* 65537 */
	private static final int KEY_LENGTH = 2048;

	private final SecureRandom random = new SecureRandom();

	public AsymmetricCipherKeyPair generate() {
		RSAKeyPairGenerator keyGenerator = new RSAKeyPairGenerator();
		keyGenerator.init(new RSAKeyGenerationParameters(
			PUBLIC_EXPONENT, random, KEY_LENGTH, 95 /* certainty in % */
		));
		return keyGenerator.generateKeyPair();
	}
}

@@ -38,16 +38,122 @@
 .cert;%0A%0A
+import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;%0Aimport org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;%0A
 import o
@@ -333,248 +333,1352 @@
 rs;%0A
-%0Aimport java.math.BigInteger;%0Aimport java.security.SecureRandom;%0A%0Apublic final class KeyPairGenerator %7B%0A%09private static final BigInteger PUBLIC_EXPONENT = new BigInteger(%2210001%22, 16); /* 65537 */%0A%09private static final int KEY_LENGTH = 2048;
+import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;%0Aimport org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;%0Aimport org.bouncycastle.jcajce.DefaultJcaJceHelper;%0A%0Aimport java.io.IOException;%0Aimport java.math.BigInteger;%0Aimport java.security.KeyFactory;%0Aimport java.security.KeyPair;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.SecureRandom;%0Aimport java.security.spec.InvalidKeySpecException;%0Aimport java.security.spec.PKCS8EncodedKeySpec;%0Aimport java.security.spec.X509EncodedKeySpec;%0A%0Apublic final class KeyPairGenerator %7B%0A%09private static final BigInteger PUBLIC_EXPONENT = new BigInteger(%2210001%22, 16); /* 65537 */%0A%09private static final int KEY_LENGTH = 2048;%0A%0A%09public static KeyPair toJca(AsymmetricCipherKeyPair keyPair) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException %7B%0A%09%09SubjectPublicKeyInfo publicKey = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(keyPair.getPublic());%0A%09%09PrivateKeyInfo privateKey = PrivateKeyInfoFactory.createPrivateKeyInfo(keyPair.getPrivate());%0A%0A%09%09KeyFactory keyFactory = new DefaultJcaJceHelper().createKeyFactory(%22RSA%22); // TODO should we really assume RSA?%0A%09%09return new KeyPair(%0A%09%09%09keyFactory.generatePublic(new X509EncodedKeySpec(publicKey.getEncoded())),%0A%09%09%09keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getEncoded()))%0A%09%09);%0A%09%7D
 %0A%0A%09p

Add method to convert Bouncy Castle key pairs to Java's KeyPair object.