title: iOS using CryptoSwift Encryption AES/CBC/PKCS5PADDING Equivalent encryption with java not working properly
body: <p>I am building a swift application where I need to have AES/CBC/PKCS5PADDING Encryption type. To encrypt API params and decrypt API response.</p>
  <p>I have successfully added in android java and that works fine.
  Now I am trying to implement the same formate type of encryption 'AES/CBC/PKCS5PADDING' cipher size 16 in swift.</p>
  <p><strong>Problem</strong>: I am getting different encryption result for both android and iOS</p>
  <p>Example:
  String: <strong>Hi how are u</strong></p>
  <p>Encrypted String for android: <strong>+A3p093WWrXU3Ey9i/Lv1Q==</strong></p>
  <p>Encrypted String for swift(iOS): <strong>Bp23PQX6yaCghvKFTieJDw==</strong></p>
  <p><em><strong>Swift Implementation</strong></em></p>
  <pre><code>import Foundation
  import CommonCrypto
  
  func aesEncrypt(key: String, iv: String, message: String) throws -&gt; String{
      let data = message.data(using: .utf8)!
      // let enc = try AES(key: key, iv: iv, padding: .pkcs5).encrypt([UInt8](data))
      let enc = try AES(key: Array(key.utf8), blockMode: CBC(iv: Array(iv.utf8)), padding: .pkcs5).encrypt([UInt8](data))
      let encryptedData = Data(enc)
      return encryptedData.base64EncodedString()
  }
  
  func aesDecrypt(key: String, iv: String, message: String) throws -&gt; String {
      let data = NSData(base64Encoded: message, options: NSData.Base64DecodingOptions(rawValue: 0))!
      let dec = try AES(key: key, iv: iv, padding: .pkcs5).decrypt([UInt8](data))
      let decryptedData = Data(dec)
      return String(bytes: decryptedData.bytes, encoding: .utf8) ?? &quot;Could not decrypt&quot;
  }
  </code></pre>
  <p><em><strong>Android Implementation</strong></em></p>
  <pre><code>package com.example.uribanew.utils;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  public class EncrytData {
  
  private static String CIPHER_NAME = &quot;AES/CBC/PKCS5PADDING&quot;;
  
  private static int CIPHER_KEY_LEN = 16; //128 bits
  
  private static String KEY   = &quot;&quot;; // key to use should be 16 bytes long (128 bits)
  private static String IV    = &quot;&quot;; // initialization vector
  
  /**
   * Encrypt data using AES Cipher (CBC) with 128 bit key
   *
   * @param data - data to encrypt
   * @return encryptedData data in base64 encoding with iv attached at end after a :
   */
  public static String encrypt(String data) {
  
      try {
          IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(&quot;UTF-8&quot;));
          SecretKeySpec secretKey = new SecretKeySpec(fixKey(KEY).getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
  
          Cipher cipher = Cipher.getInstance(EncrytData.CIPHER_NAME);
          cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
  
          byte[] encryptedData = cipher.doFinal((data.getBytes()));
  
          String encryptedDataInBase64 = android.util.Base64.encodeToString(encryptedData, android.util.Base64.DEFAULT); //Base64.getEncoder().encodeToString(encryptedData);
  
          return encryptedDataInBase64;
  
      } catch (Exception ex) {
          throw new RuntimeException(ex);
      }
  }
  
  private static String fixKey(String key) {
  
      if (key.length() &lt; EncrytData.CIPHER_KEY_LEN) {
          int numPad = EncrytData.CIPHER_KEY_LEN - key.length();
  
          for (int i = 0; i &lt; numPad; i++) {
              key += &quot;0&quot;; //0 pad to len 16 bytes
          }
  
          return key;
  
      }
  
      if (key.length() &gt; EncrytData.CIPHER_KEY_LEN) {
          return key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
      }
  
      return key;
  }
  
  /**
   * Decrypt data using AES Cipher (CBC) with 128 bit key
   *
   * @param data - encrypted data with iv at the end separate by :
   * @return decrypted data string
   */
  
  public static String decrypt(String data) {
  
      try {
          IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(&quot;UTF-8&quot;));
          // SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
          SecretKeySpec secretKey = new SecretKeySpec(fixKey(KEY).getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
  
          Cipher cipher = Cipher.getInstance(EncrytData.CIPHER_NAME);
          cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
  
          byte[] decodedEncryptedData = android.util.Base64.decode(data, android.util.Base64.DEFAULT); //Base64.getDecoder().decode(parts[0]);
  
          byte[] original = cipher.doFinal(decodedEncryptedData);
  
          return new String(original);
      } catch (Exception ex) {
          throw new RuntimeException(ex);
      }
  }
  }
  </code></pre>
  <p>Please let me know why with the same encryption type I am getting different encrypted string for both android and iOS. Let me know what I am doing wrong</p>