Take the 2-minute tour ×

I need help with this problem. On the internet I found many examples to encrypt on android with rsa and decrypt in php. In my case I generate the key pair on android, sending php public key, then encrypt with that key in PHP, and decrypt in android with the private key. I use Spongy Castle in android and Phpseclib in php.

In android I use the class org.spongycastle.util.encoders.Base64

Generate key pair:

KeyPair kPair = generator.generateKeyPair();
byte[] privateKeyEnc = kPair.getPrivate().getEncoded();
byte[] publicKeyEnc = kPair.getPublic().getEncoded();
String publicKey = new String(Base64.encode(publicKeyEnc)); //this key is send to php
String privateKey = new String(Base64.encode(privateKeyEnc));

Encrypt in php:

$message = "...";
$publicKey = "..."; //key received from android
$rsa = new Crypt_RSA();
$key = "-----BEGIN PUBLIC KEY-----\n" . base64_decode($publicKey) 
            . '-----END PUBLIC KEY-----';
$messageEncrypt = base64_encode($rsa->encrypt($message)); //this message is send to android

Decrypt in android:

String message = "..."; //this received from php
String privateKey = "..."; //this key was generated in android
byte[] privateKeyBytes = Base64.decode(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory fact = KeyFactory.getInstance("RSA", "SC");
PrivateKey priv = fact.generatePrivate(keySpec);
Cipher rsaCipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "SC");
rsaCipher.init(Cipher.DECRYPT_MODE, priv);
byte[] messageDecryptBytes = rsaCipher.doFinal(message);
String messageDecrypt = new String(Base64.encode(messageDecryptBytes));

In android I get the following error:

Exception:java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block

As I read the cause of the error is that the bytes in the message to decrypt not follow the expected format for rsa. If that is the case then the error would be in php when encrypting.

I can successfully run on android encrypt and decrypt in php. But I have not found examples in php to encrypt using a public key generated on android.

share|improve this question
shouldn't it be new String(Base64. **DE** code(...))? Looks like you're double ENCODING (once in php, once in android) –  Marc B Jun 13 at 20:24
I added a line where I make a decoding of the received message from php. Decrypt in android: ... rsaCipher.init(Cipher.DECRYPT_MODE, priv); byte[] messageDecoded = Base64.decode(message); // Added this line byte[] messageDecryptBytes = rsaCipher.doFinal(messageDecoded); ... Now does not return error, but the decrypted message does not match the original message. For example sending "Hello World" the decrypted message is "SG9sysbTDw5KBW ==". –  user2502565 Jun 13 at 20:48
That looks like a base64 encoded string. so try base64_decode('SG9...') –  Marc B Jun 13 at 20:50
It work. In the last line of decrypt code in android I change that line: String messageDecrypt = new String(Base64.encode(messageDecryptBytes)); to that line: String messageDecrypt = new String(messageDecryptBytes); I was doing incorrectly encoding to the decrypted message. Thanks for the help. –  user2502565 Jun 13 at 21:00

Your Answer



Browse other questions tagged or ask your own question.