title: RSA encrypt in php to RSA decrypt in JAva
body: <p>at the moment im trying to encrypt with rsa in php with a public key generated in an android app and then decrypt in android app again.</p>
  
  <p>My code to generate the keys in android is:</p>
  
  <pre><code>KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(1024);
  KeyPair kp = kpg.generateKeyPair();
  PublicKey publicKey = kp.getPublic();
  PrivateKey privateKey = kp.getPrivate();
  </code></pre>
  
  <p>With that keys i can en- and decrypt very well. The pub key looks like this: </p>
  
  <pre><code>OpenSSLRSAPublicKey{modulus=9ee9f82dd8429d9fa7f091c1d375b9c289bcf2c39ec57e175a2998b4bdd083465ef0fe6c7955c821b7e883929d017a9164a60290f1622f664a72096f5d2ffda7c7825c3d657c2d13d177445fa6cdd5d68b96346006a96040f5b09baae56d0c3efeaa77d57602f69018f5cefd60cb5c71b6b6f8a4b0472e8740367266917d8c13,publicExponent=10001}
  </code></pre>
  
  <p>In php im taking the modulus and exponent, creating a encrypted string with phpseclib 1.0</p>
  
  <pre><code>$rsa = new Crypt_RSA();
              // $rsa-&gt;createKey();
              $m = "9ee9f82dd8429d9fa7f091c1d375b9c289bcf2c39ec57e175a2998b4bdd083465ef0fe6c7955c821b7e883929d017a9164a60290f1622f664a72096f5d2ffda7c7825c3d657c2d13d177445fa6cdd5d68b96346006a96040f5b09baae56d0c3efeaa77d57602f69018f5cefd60cb5c71b6b6f8a4b0472e8740367266917d8c13";
              $e = "10001";
              $data = "hallo";                
              $modulus = new Math_BigInteger($m, 16);
              $exponent = new Math_BigInteger($e, 16);
              $rsa-&gt;loadKey(array('n' =&gt; $modulus, 'e' =&gt; $exponent));
              $messageEncrypt = $rsa-&gt;encrypt($data);
  </code></pre>
  
  <p>In Android again, im loading the key, and decrypting it like this:</p>
  
  <pre><code>Cipher cipher1 = Cipher.getInstance("RSA");
  cipher1.init(Cipher.DECRYPT_MODE, privateKey);
  byte[] decryptedBytes = cipher1.doFinal(encrypted.getBytes());
  String decrypted = new String(decryptedBytes);
  </code></pre>
  
  <p>Im always getting a wrong decrypted plaintext or a " Caused by: java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block" error message from Android.</p>
  
  <p>What i think: The problem is the encoded transfer. That php outputs a different encoded version as java uses. So I tried a lot of different ways. I tried to convert the output to String/bin/hex/byte. Then transfer it, with socket or with copy+paste directly in the Code. Convert it back from hex/bin... to a byte[] and try to decode it. Nothing works...</p>
  
  <p>Anyone has a solution for this?</p>