Is there any way that we can write this public, private key generated through RSAKeyPairGenerator to file system (I know this is not a good idea but I need to check whether this can be done)?
RSAKeyPairGenerator will result in CipherParameters and I am using bouncy castle security provide for java.
Code is as follows to get my keys.
RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();
keyPairGenerator.init(new RSAKeyGenerationParameters(new BigInteger(
"10001", 16), new SecureRandom(), 1024, 80));
AsymmetricCipherKeyPair keys = keyPairGenerator.generateKeyPair();
CipherParameters pubKey = keys.getPublic();
CipherParameters privKey = keys.getPrivate();
Cast the parameters up to the actual type (print out privKey.getClass().getName() to find out what it is). The RSA private key will be a type of RSAPrivateCrtKeyParameters. Once you've got that you can simply retrieve the BigInteger's that make up to private key. Those should be serializable. If you want to encode it in PKCS#8/PKCS#1 then you should create a JCE compatible RSACrtPrivateKey instance and call getEncoded().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With