title: Decrypting message with RSA-OAEP and Android Keystore: IllegalBlockSizeException
body: <p>I have the following code inside a <code>try</code> block that should generate a RSA public/private keypair use the public key to encrypt a message and decrypt again with the private key:</p>
  
  <pre><code>KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
           KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
  keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(
                   "key1",
                   KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
                   .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                   .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
                   .build());
  KeyPair keyPair = keyPairGenerator.generateKeyPair();
  byte[] src = "hello world".getBytes();
  
  Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
  cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
  byte[] cipherData = cipher.doFinal(src);
  
  Cipher cipher2 = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
  cipher2.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
  byte[] msg = cipher2.doFinal(cipherData);
  
  </code></pre>
  
  <p>Taken mostly from <a href="https://stackoverflow.com/q/33754129">here</a> and <a href="https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec#example:-rsa-key-pair-for-encryptiondecryption-using-rsa-oaep" rel="nofollow noreferrer">here</a>.</p>
  
  <p>The final line throws an exception of type <code>javax.crypto.IllegalBlockSizeException</code> with no message/further details. The three lines in <code>logcat</code> before the exception are</p>
  
  <pre><code> E keymaster1_device: Finish send cmd failed
   E keymaster1_device: ret: 0
   E keymaster1_device: resp-&gt;status: -1000
  </code></pre>
  
  <p>in case that matters at all.</p>
  
  <p>Does anyone have an idea what could be going wrong? </p>
  
  <p>Using <code>minSdkVersion 23</code></p>
  
  <p>Edit:
  I just realised, if I use PKCS#1 v1.5 padding it works. That helps me for now, but I'd still like to try get it work with OAEP.</p>