title: Android AES decryption and data from iOS:javax.crypto.BadPaddingException: pad block corrupted
body: <p>I tried to decrypt a backup on Android which is sent from <code>iOS</code>, and the exception <code>javax.crypto.BadPaddingException: pad block corrupted</code> is showed at method doFinal.</p>
  
  <pre><code>public  String decrypt(byte[] cipherText, SecretKey key, byte [] initialVector) throws Exception {
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
      IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
      cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
      cipherText = cipher.doFinal(cipherText);
  
      return new String(cipherText, "UTF-8");
  }
  </code></pre>
  
  <p>The key and initialVector are sent from iOS in base64 string. Related code:</p>
  
  <pre><code>public static byte[] decodeWebSafe(String s) throws Base64DecoderException {
      byte[] bytes = s.getBytes();
      return decodeWebSafe(bytes, 0, bytes.length);
  }
  
  byte[] iv = Base64.decodeWebSafe(enciv);
  byte[] salt = Base64.decodeWebSafe(encsalt);
  byte[] data = Base64.decodeWebSafe(encdata);
  SecretKey key = Security.getExistingKey(password, salt);
  String original = aes.decrypt(data, key, iv);
  </code></pre>
  
  <p>And about the Security.getExistingKey:</p>
  
  <pre><code>public static SecretKey getExistingKey(String password, byte[] salt) throws Exception{
      SecretKey key= null;
      KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  
      byte[] keyBytes=new byte[32]; 
      keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
      key= new SecretKeySpec(keyBytes, "AES");
  
      return key;
  }
  </code></pre>
  
  <p>Thx for any solutions.</p>
  
  <p>P.S.This is how we set the encryption in iOS:</p>
  
  <pre><code>CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
  kCCOptionPKCS7Padding,
  self.encryptionKey.bytes, kCCKeySizeAES128,
  self.encryptionIV.bytes, [rawData bytes], dataLength, 
  /* input */buffer, bufferSize, /* output */&amp;numBytesEncrypted);
  </code></pre>
  
  <p>the key and IV derivation method:</p>
  
  <pre><code>(NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt {
  NSMutableData *
  derivedKey = [NSMutableData dataWithLength:kCCKeySizeAES128];
  
  int result = CCKeyDerivationPBKDF(kCCPBKDF2,            // algorithm
                                password.UTF8String, 
                                password.length,  
                                salt.bytes,           // salt
                                salt.length,          // saltLen
                                kCCPRFHmacAlgSHA1,    // PRF
                                kPBKDFRounds,         // rounds
                                derivedKey.mutableBytes, // derivedKey
                                derivedKey.length); // derivedKeyLen
  }
  </code></pre>