title: javax.crypto.IllegalBlockSizeException Caused by: android.security.KeyStoreException: Invalid input length
body: <p>I'm currently making a small Android program that stores encrypted file and can later load+decrypt it. I am having an issue here with my code and can't seem to find the right answer that would help.
  My code:</p>
  
  <pre><code>private void prepKeys() throws NoSuchAlgorithmException, NoSuchPaddingException,
          InvalidKeyException, IllegalBlockSizeException, BadPaddingException, KeyStoreException, IOException, CertificateException, UnrecoverableEntryException, InvalidAlgorithmParameterException, NoSuchProviderException {
      String alias = "TESTINGKEY10";
  
      KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
      ks.load(null);
      kpg = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
  
      if(ks.containsAlias(alias)) {
          KeyStore.Entry entry = ks.getEntry(alias, null);
  
          privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
          publicKey = ks.getCertificate(alias).getPublicKey();
      }
      else {
          kpg.initialize(new KeyGenParameterSpec.Builder(
                  alias,
                  KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                  .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                  .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                  .setKeySize(1024)
                  .build());
  
          kp = kpg.generateKeyPair();
          publicKey = kp.getPublic();
          privateKey = kp.getPrivate();
  
      }
  }
  
  public byte[] RSAEncrypt(final String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,
          InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
      cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
      cipher.init(Cipher.ENCRYPT_MODE, publicKey);
      encryptedBytes = cipher.doFinal(plain.getBytes());
      return encryptedBytes;
  }
  
  @RequiresApi(api = Build.VERSION_CODES.O)
  public String RSADecrypt(final byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
          InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
      cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
      cipher1.init(Cipher.DECRYPT_MODE, privateKey);
      decryptedBytes = cipher1.doFinal(encryptedBytes);
      decrypted = new String(decryptedBytes);
      return decrypted;
  }
  </code></pre>
  
  <p>So it loads or generates keys with prepKeys() on app load and the encrypts/decrypts text when required.</p>
  
  <p>The error I am getting is:</p>
  
  <pre><code>W/System.err: javax.crypto.IllegalBlockSizeException
  W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:513)
  W/System.err:     at javax.crypto.Cipher.doFinal(Cipher.java:1741)
  W/System.err:     at com.example.aizaku.myapplication.MainActivity.RSADecrypt(MainActivity.java:113)
  W/System.err:     at com.example.aizaku.myapplication.MainActivity$1.onClick(MainActivity.java:202)
  W/System.err:     at android.view.View.performClick(View.java:6294)
  W/System.err:     at android.view.View$PerformClick.run(View.java:24770)
  W/System.err:     at android.os.Handler.handleCallback(Handler.java:790)
  W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
  W/System.err:     at android.os.Looper.loop(Looper.java:164)
  W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
  W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  W/System.err: Caused by: android.security.KeyStoreException: Invalid input length
  W/System.err:     at android.security.KeyStore.getKeyStoreException(KeyStore.java:697)
  W/System.err:     at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.update(KeyStoreCryptoOperationChunkedStreamer.java:132)
  W/System.err:     at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:217)
  W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
  W/System.err:   ... 12 more
  </code></pre>
  
  <p>It happens when trying to decrypt data (even small encrypted text "abc") at line</p>
  
  <pre><code>decryptedBytes = cipher1.doFinal(encryptedBytes);
  </code></pre>
  
  <p>Please help me find the answer :)</p>