So I have a PublicKey in Java, and I need to send a post request to the server with the key in it, then read the key server side, and send some data encrypted with it!
I succeeded at:
- Generating the keypair (duh)
- Encrypting and decrypting data with it in the Java program
- Maybe: making a pem formatted key from it, I'm not sure
String phpPublic = ("-----BEGIN PUBLIC KEY-----"+Base64.encodeToString(MainActivity.instance.rsa.readPublicKeyFromFile(MainActivity.instance.rsa.PUBLIC_CLIENT_KEY_FILE).getEncoded(),Base64.DEFAULT)+"-----END PUBLIC KEY-----");
I think this does it, but I'm not sure! And would it be the same process to convert private keys to pem, just to make PUBLIC -> PRIVATE in the header and footer.
I don't know if converting to pem is really necessary, if it's not, please suggest a better way to do it.
This is how I do it in PHP, but I'm 99% sure it is wrong
$PubKey = openssl_pkey_get_public($publicPem);
$encrypted;
openssl_public_encrypt($toEncode, $encrypted, $PubKey);
echo $encrypted;