Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing CipherParameters to file system

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();
like image 704
Dilshan Avatar asked Feb 27 '26 12:02

Dilshan


1 Answers

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().

like image 165
Maarten Bodewes Avatar answered Mar 02 '26 00:03

Maarten Bodewes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!