Search code examples
phpandroidencryptionphp-openssl

What is android equivalent of openssl_public_encrypt in php?


I am a PHP developer(not an android developer). I outsourced the development of an Android app, and for communication between the app and my PHP server, I intend to have asymmetrical encryption(public-private key encryption).

At PHP level, I know openssl_public_encrypt and openssl_private_decrypt. I told the android developer to use OpenSSL public encryption and RSA encryption to use in-app, but it is not working.

I tried searching, but couldn't find any fruitful results.

Can anyone help me with this, so that I can tell my developer to use a particular method or something?


Solution

  • I found out the answer. openssl_public_encrypt uses RSA algorithm and same can be used in Android in following way:

    Cipher cipher = Cipher.getInstance(RSA);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    

    What I was doing wrong was using default encryption mode on both sides, PHP as well as Android, which was wrong as they differed.

    For more details read : https://stackoverflow.com/a/17820910/3333052