title: Android 4.2 broke my encrypt/decrypt code and the provided solutions don't work
body: <p>First of all, I've already seen 
  <a href="https://stackoverflow.com/questions/13389870/android-4-2-broke-my-aes-encrypt-decrypt-code">Android 4.2 broke my AES encrypt/decrypt code</a>
  and
  <a href="https://stackoverflow.com/questions/13383006/encryption-error-on-android-4-2">Encryption error on Android 4.2</a>
  and the provided solution:</p>
  
  <pre><code>SecureRandom sr = null;
  if (android.os.Build.VERSION.SDK_INT &gt;= JELLY_BEAN_4_2) {
      sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
  } else {
      sr = SecureRandom.getInstance("SHA1PRNG");
  }
  </code></pre>
  
  <p>doesn't work for me, because, when decoding data encrypted in Android&lt;4.2 in Android 4.2, I get:</p>
  
  <pre><code>javax.crypto.BadPaddingException: pad block corrupted
  at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:709)
  </code></pre>
  
  <p>My code is quite simple, and was working until Android 4.2:</p>
  
  <pre><code>public static byte[] encrypt(byte[] data, String seed) throws Exception {
  
      KeyGenerator keygen = KeyGenerator.getInstance("AES");
      SecureRandom secrand = SecureRandom.getInstance("SHA1PRNG");
      secrand.setSeed(seed.getBytes());
      keygen.init(128, secrand);
  
      SecretKey seckey = keygen.generateKey();
      byte[] rawKey = seckey.getEncoded();
  
      SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      return cipher.doFinal(data);
  }
  
  public static byte[] decrypt(byte[] data, String seed) throws Exception {
  
      KeyGenerator keygen = KeyGenerator.getInstance("AES");
      SecureRandom secrand = SecureRandom.getInstance("SHA1PRNG");
      secrand.setSeed(seed.getBytes());
      keygen.init(128, secrand);
  
      SecretKey seckey = keygen.generateKey();
      byte[] rawKey = seckey.getEncoded();
  
      SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, skeySpec);
      return cipher.doFinal(data);
  }
  </code></pre>
  
  <p>My guess is that the default provider wasn't the only thing that changed in Android 4.2, otherwise my code would work with the proposed solution.</p>
  
  <p>My code was based on some post I found here at StackOverflow a long time ago; I see that it differs from the mentioned posts as it just crypts and decrypts byte arrays, whereas the others solutions crypt and decrypt Strings (HEX Strings, I think).</p>
  
  <p>Does it have to do with the seed? Does it have a min/max length, restriction of chars, etc?</p>
  
  <p>Any idea / solution?</p>
  
  <p><strong>EDIT</strong>:
  After a lot of tests, I see that there are 2 problems:</p>
  
  <ol>
  <li><p>The provider changed in Android 4.2 (API 17) -> This one is easy to fix, just apply the solution I mentioned at top of the post</p></li>
  <li><p>BouncyCastle changed from 1.34 to 1.45 in Android 2.2 (API 8)->Android2.3 (API 9), so the decryption problem I previously told is the same as described here: <a href="https://stackoverflow.com/questions/4405334/bouncycastle-aes-error-when-upgrading-to-1-45">BouncyCastle AES error when upgrading to 1.45</a> </p></li>
  </ol>
  
  <p>So now the question is: <strong>is there any way to recover data crypted in BouncyCastle 1.34 in BouncyCastle 1.45+?</strong></p>