title: PHP & Java Decrpytion error using PhpSecLib and BouncyCastle
body: <p>I'm trying to encrypt stuff in java using the public key generated by my PHP:</p>
  
  <p>PHP Code</p>
  
  <pre><code>    $rsa = new Crypt_RSA();
      $rsa-&gt;setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
      $rsa-&gt;setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
      $rsa-&gt;setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
  
  
      $keys = $rsa-&gt;createKey(1024);
      extract($keys);
  
      echo (base64_encode($publickey));
  </code></pre>
  
  <p>For testing purposes, I've set aside a keypair (base64) of the above format. </p>
  
  <p>I retrieve my Public Key in java and base64 decode it.</p>
  
  <pre><code>    String publicKeyDecoded = new String(Base64.decode(publicKey));
      PEMParser pr = new PEMParser(new StringReader(publicKeyDecoded));
      Object obj = pr.readObject();
      pr.close();
      SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) obj;
      AsymmetricKeyParameter askp = PublicKeyFactory.createKey(spki);
  
      AsymmetricBlockCipher e = new RSAEngine();
      e = new org.bouncycastle.crypto.encodings.PKCS1Encoding(e);
      e.init(true, askp);
  
      byte[] messageBytes = plainText.getBytes();
      byte[] encryptedData = e.processBlock(messageBytes, 0, messageBytes.length);
      byte[] encryptedDataBase = Base64.encode(encryptedData);
  </code></pre>
  
  <p>I send the Base64 encrypted plaintext back to PHP for decryption using the following:</p>
  
  <pre><code>    $rsa-&gt;loadKey($privatekey) or die ("Cant load");
      echo $rsa-&gt;decrypt($cipher);        
  </code></pre>
  
  <p>It's unable to decrpyt my encoded message and throws me the error:</p>
  
  <pre><code>    Decryption error in &lt;b&gt;/opt/lampp/htdocs/Crypt/RSA.php&lt;/b&gt; on line &lt;b&gt;2120&lt;/b&gt;
  </code></pre>
  
  <p>Can someone point me to the right direction? It's been hours since I'm trying to figure this out.</p>
  
  <p>I'm using a hardcoded keypair so I guess there's no question of my keys being wrong...</p>