I want to implement a RSA algorithm to encrypt an image (byte[]). To generate my two keys I used this piece of code :
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(512);
keyPair = keygen.generateKeyPair();
Once public and private key are generated, I would like to show them to the user so he can distribute the public key and use the private key to decode. How can I get back those key?
Using keygen.getPrivateKey() and keygen.getPublicKey() give me all the information of the RSA algorithm, not only the keys I need.
Thanks
Via the Relevant KeySpec classes, you can call the getModulus() and getPublicExponent()/getPrivateExponent() methods to pull out the key components:
In case it’s useful, I wrote a few articles a while back dealing with some of the details of RSA encryption in Java (and Java-based cryptography generally.