title: SHA256withRSA sign from PHP verify from JAVA
body: <p>For my current project I have to send a signature from PHP to Java application. I am using Crypt/RSA right now for signing my data. </p>
  
  <p>For test I am signing just "abc" with following code : </p>
  
  <pre><code>$rsa = new Crypt_RSA();
  $plaintext = 'abc';
  
      $rsa-&gt;loadKey("MIICXgIBAAKBgQDjh+hNsqJe566JO0Sg7Iq5H1AdkauACdd8QMLp9YNY0HPslVH0
  rXaOFo0zgH0Ktu/Ku3lS1lfxbFQAY8b6ywZKvu4eoxlnEwuBwy09CG+3ZiVLBjCj
  TZHA/KOkpVLa+tA6KsoP6zv/xI/ACkSCxPGR0q3SiRuhXV/6tacoKxUYnwIDAQAB
  AoGBAIC00GOjONYWmFRoglnFdHNjkx4m2KyE5LAUsi1GBBapU+nwTXvq47VcbGNF
  u3XkJaC4i9igBv86GApgZp5XWia86On/Lz9NR4fB2EFP6Ydy84GfCDNNvkism4BR
  aA+eYdNiQ3Wfyi98ZpUi+rPsoI6Cid4eSkCC4poTUaqzMkiBAkEA9Gn1oIlUEoVI
  q/u5Y9vflXRDt95AA9AokJkQj7XTNjsz8ypU8TO6D6ZykpcbK6zjU0UJsQiC3dKj
  AgmAR2VzYwJBAO5RETMAyDnR+5g+MtHpwGqGdY4dq0j4y4CsdtOYKWwSTh3VQy+C
  eghJoyPRfIpulw2Mk/l+occEI0ohJl0+UJUCQQDSZtjVLwMZwnUx4EvSw/ewL9sP
  0Jpo7evNtoaEQDEncUWiYeGnljDowg/FU6FHMtiq2TajmMEXdflvioBMdfAjAkEA
  3TB60SbJr/i4Fo6sJm5ZO8W+eAALiTf50VzBERTqZTb8L+5PZFoqn2SROV5mxClu
  o5G1idzBlHC/vD7WV7bNnQJAd0FrxaMBurJ4Uv/B8TDP+eeBdB7d9rKw0+TVlcel
  cbpIz6BIP6+nmsgy6dbDRnx0eC/MgF2EU0wrCu1DK0PyWA==");
      $rsa-&gt;setHash("sha256");
      $signature = $rsa-&gt;sign($plaintext);
  
  $signature_encoding = mb_convert_encoding($signature, "UTF-8");
      error_log("signature encoded in UTF-8 :" . $signature_encoding);
  
      $encoded_sign = base64_encode($signature_encoding);
      error_log("encoded sign for abc: " . $encoded_sign);
  </code></pre>
  
  <p>I can verify the signature from php code. But when it comes to verifying from JAVA, i was not successfull. Here is the java code that does the verify operation : </p>
  
  <pre><code>public boolean verify(String signed, String data, PubKey pubKey) throws Exception{
  
      PublicKey publicKey = jceProvider.generateRSAPublicKeyFromX509(
              base64.decode(pubKey.getEncodedKey())
      );
  
      byte[] signature = base64.decode(signed);
      byte[] verifier = data.getBytes(Charset.forName("UTF-8"));
  
      return jceProvider.verify(signature, verifier, publicKey);
  
  }
  
  public class JCEProvider {
  
      public boolean verify (byte[] signature, byte[] verifier, PublicKey publicKey) throws Exception{
  
          Signature rsaSignature = Signature.getInstance("SHA256withRSA");
  
          rsaSignature.initVerify(publicKey);
          rsaSignature.update(verifier);
  
          return rsaSignature.verify(signature);
  
      }
  </code></pre>
  
  <p>I dont think it is because of keys, I can already verify it from PHP as I told before. There is something that I miss about PHP encoding or byte streams but I am lost for the moment. </p>
  
  <p>Any help would be appreciated.</p>