title: Encryption PHP, Decryption Java
body: <p>I have a web-service in php that generates a keypair to encrypt a message, and one application in java that retrives the privatekey and decrypt the message.</p>
  
  <p>For php I'm using <a href="http://phpseclib.sourceforge.net/" rel="nofollow">http://phpseclib.sourceforge.net/</a> and have this two files:</p>
  
  <p>keypair.php</p>
  
  <pre><code>&lt;?php
  
  set_time_limit(0);
  if( file_exists('private.key') )
  {
      echo file_get_contents('private.key');
  }
  else
  {
      include('Crypt/RSA.php');
      $rsa = new Crypt_RSA();
      $rsa-&gt;createKey();
      $res = $rsa-&gt;createKey();
  
      $privateKey = $res['privatekey'];
      $publicKey  = $res['publickey'];
  
      file_put_contents('public.key', $publicKey);
      file_put_contents('private.key', $privateKey);
  }
  
  ?&gt;
  </code></pre>
  
  <p>encrypt.php</p>
  
  <pre><code>&lt;?php
  
  include('Crypt/RSA.php');
  
  //header("Content-type: text/plain");
  
  set_time_limit(0);
  $rsa = new Crypt_RSA();
  $rsa-&gt;setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
  $rsa-&gt;loadKey(file_get_contents('public.key')); // public key
  
  $plaintext = 'Hello World!';
  $ciphertext = $rsa-&gt;encrypt($plaintext);
  
  echo base64_encode($ciphertext);
  
  ?&gt;
  </code></pre>
  
  <p>and in java I have this code:</p>
  
  <pre><code>package com.example.app;
  
  import java.io.DataInputStream;
  import java.net.URL;
  import java.security.Security;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
  
  import sun.misc.BASE64Decoder;
  
  public class MainClass {
  
      /**
       * @param args
       */
      public static void main(String[] args)
      {
          Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  
          try {
              BASE64Decoder decoder   = new BASE64Decoder();
              String b64PrivateKey    = getContents("http://localhost/api/keypair.php").trim();
              String b64EncryptedStr  = getContents("http://localhost/api/encrypt.php").trim();
  
              System.out.println("PrivateKey (b64): " + b64PrivateKey);
              System.out.println(" Encrypted (b64): " + b64EncryptedStr);
  
              SecretKeySpec privateKey    = new SecretKeySpec( decoder.decodeBuffer(b64PrivateKey) , "AES");
              Cipher cipher               = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
              cipher.init(Cipher.DECRYPT_MODE, privateKey);
  
              byte[] plainText            = decoder.decodeBuffer(b64EncryptedStr);
  
              System.out.println("         Message: " + plainText);
          }
          catch( Exception e )
          {
              System.out.println("           Error: " + e.getMessage());
          }
  
      }
  
      public static String getContents(String url)
      {
          try {
              String result = "";
              String line;
              URL u = new URL(url);
              DataInputStream theHTML = new DataInputStream(u.openStream());
              while ((line = theHTML.readLine()) != null)
                  result = result + "\n" + line;
  
              return result;
          }
          catch(Exception e){}
  
          return "";
      }
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Why I'm having a exception saying "not an RSA key!"?</li>
  <li>How can I improve this code? I have used base64 to avoid encoding and comunication errors between Java and PHP.</li>
  <li>This concept is correct? I mean, I'm using it correctly?</li>
  </ol>