title: RSA Encryption in JavaCard
body: <p>I hope anyone can help me with my problem. 
  I'm developing a javacard applet and my task is to encrypt a secret (stored in the applet) with the public key of a server. The returning cipher will be send to a server afterwrds - php script on the server - where the cipher should be decrypted with the servers private key again.</p>
  
  <p>And The problem is that the decryption in the php script (openssl_private_decrypt(...)) doesn't return anything, so it fails anywhere.</p>
  
  <p>PS: I also saw some javacard RSA encryption examples and nearly all of them are using a private key for encryption which means in my opinion that the data is only be signed, not encrypted, am I right?</p>
  
  <p>Here is some example code from the javacard applet:</p>
  
  <pre><code>byte[] secret ={ (byte)0x01,(byte)0x02,(byte)0x03,(byte)0x04}
  public Crypto() {
      RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyBuilder.buildKey(
      KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_2048, false);
      Cipher rsaCipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
      encryptSecretWithPubKey();
  }
  .....
  // here are some methods for setting the public key
  .....
  
  public byte[] encryptSecretWithPubKey() {
      if (!isRSAKeyInitialized) {
          ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
      }
      // make sure the blocks size which should be encrypted is not longer
      // than 53 bytes; see ALG_RSA_PKCS1 definition for encryption.
      if (secret.length &gt;= rsaKeyModulus.length - 11) {
         // normally not the case, so ignore it for this test
      }
      rsaCipher.init(rsaPublicKey, Cipher.MODE_ENCRYPT);
      final byte[] encryptedMsg = JCSystem.makeTransientByteArray(
              (short) 256, JCSystem.CLEAR_ON_DESELECT);
  
      if (rsaPublicKey.isInitialized())
          rsaCipher.doFinal(secret, (short) 0,
                  (short) secret.length, encryptedMsg, (short) 0);
      return encryptedMsg; 
  }
  </code></pre>
  
  <p>And here are the function I use for decryption in the php script:</p>
  
  <pre><code>public function decrypt($data)
  { // data is in binary format
      if (openssl_private_decrypt($data, $decrypted, $this-&gt;privkey))
          $data = $decrypted;
      else
          // openssl function fails here, so no plain data is returned
          $data = '';
      return $data;
  }
  </code></pre>
  
  <p>I hope you can help what I'm ding wrong here, or might it be a bug in the Cipher class what I'm not expecting :)</p>
  
  <p>EDIT: 
  The openssl decryption on the server throws following error message:
  <code>error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02</code>
  The <a href="http://hustoknow.blogspot.ca/2013/01/rsa-block-type-is-not-02-error.html" rel="nofollow">error</a> means, that the block type is wrong. Im using the public key in the applet so the block Type should be 02 but it isnt... </p>
  
  <p>EDIT:</p>
  
  <p><strong>The problem is solved now</strong>. It was a programming error of my own. The modulus was not set correctly (the last byte was always set to zero) so the decryption doesn't work afterwards. </p>