title: SWIFT AES Encrypt and Decrypt - gets different results
body: <p>I'm in the process of implementing AES encryption in swift.
  The encryption decryption for java and C# is working properly. </p>
  
  <p>In swift I am getting different results than the actual one. 
  While debugging, I noticed Java uses sign int by default. 
  So I Implemented the same way, with that I am able to verify  derivedKey is same in both application(Java and Swift).
  But while creating the keyData and ivData, it looses the signed data.Not sure thats creating the issue.</p>
  
  <p>I have  tried the below code explained in <a href="https://stackoverflow.com/questions/43715416/aes-encryption-net-to-swift">AES Encryption .net to swift</a></p>
  
  <pre><code>func decrypt(encryptedText: String, keys :String)  -&gt; String{
      let encryptedData = encryptedText.data(using: .utf16LittleEndian)
      let derivedKey = generateDerivedKey(keyString: keys)
      let key = Array(derivedKey[0..&lt;32])
      let iv = Array(derivedKey[32..&lt;48])   
      let keyData = Data(bytes: key, count: key.count)
      let ivData = Data(bytes: iv, count: iv.count)        
      let decryptedData = testDeCrypt(data: encryptedData!, keyData: keyData, ivData: ivData, operation: kCCDecrypt)      
  
      return String(bytes: decryptedData, encoding: .unicode)!
  }
  
  func generateDerivedKey(keyString :String) -&gt; [Int8] {
      let salt: [UInt8] = [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]
      var key = [UInt8](repeating: 0, count: 48)
      CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), keyString, keyString.utf8.count, salt, salt.count, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1), 1000, &amp;key, 48)
      let derivedKey : [Int8] = key.map {Int8(bitPattern: $0)}
  
      return derivedKey
  }
  
  func testDeCrypt(data: Data, keyData: Data, ivData: Data, operation: Int) -&gt; Data {
      assert(keyData.count == Int(kCCKeySizeAES128) || keyData.count == Int(kCCKeySizeAES192) || keyData.count == Int(kCCKeySizeAES256))
      var decryptedData = Data(count: data.count)
      var num_bytes_decrypted: size_t = 0
      let operation = CCOperation(operation)
      let algoritm = CCAlgorithm(kCCAlgorithmAES)
      let options = CCOptions(kCCOptionPKCS7Padding)  
      let decryptedDataCount = decryptedData.count
      let cryptoStatus = keyData.withUnsafeBytes {keyDataBytes in
          ivData.withUnsafeBytes {ivDataBytes in
              data.withUnsafeBytes {dataBytes in
                  decryptedData.withUnsafeMutableBytes {decryptedDataBytes in
                      CCCrypt(operation, algoritm, options, keyDataBytes, keyData.count, ivDataBytes, dataBytes, data.count, decryptedDataBytes, decryptedDataCount, &amp;num_bytes_decrypted)
                  }
              }
          }
      }      
      if cryptoStatus == CCCryptorStatus(kCCSuccess) {
          decryptedData.count = num_bytes_decrypted
          return decryptedData
      } else {
          return Data()
      }
  }
  </code></pre>
  
  <p>Java Code</p>
  
  <pre><code>public static String aesDecrypt(String text, String key) {
      byte[] decValue = null;
      try {
          byte[] salt = new byte[] { 0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D,
                  0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
          SecretKeyFactory factory = SecretKeyFactory
                  .getInstance("PBKDF2WithHmacSHA1");
          PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray(), salt,
                  1000, 384);
  
          Key secretKey = factory.generateSecret(pbeKeySpec);
          byte[] keys = new byte[32];
          byte[] iv = new byte[16];
          System.arraycopy(secretKey.getEncoded(), 0, keys, 0, 32);
          System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);
  
          SecretKeySpec secretSpec = new SecretKeySpec(keys, "AES");
  
          AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  
          try {
              cipher.init(Cipher.DECRYPT_MODE, secretSpec, ivSpec);
          } catch (InvalidKeyException e) {
  
          } catch (InvalidAlgorithmParameterException e) {
          }
  
          org.apache.commons.codec.binary.Base64 decoder = new org.apache.commons.codec.binary.Base64();
          byte[] decodedValue = decoder.decode(text.getBytes());
  
          decValue = cipher.doFinal(decodedValue);
  
      } catch (Exception e) {
      }
  
      if (decValue != null) {
          return new String(decValue, Charset.forName("UTF_16LE"));
      } else {
          return null;
      }
  }
  </code></pre>
  
  <p>Test Data
  Key: ”ThisIsATestPassword444Encryption" 
  text : "TestStringToEncrypt" </p>
  
  <p>Java Output
  encoded cipher Text : [97, 47, 77, 79, 118, 111, 79, 70, 47, 87, 90, 67, 81, 98, 51, 74, 83, 88, 97, 68, 84, 105, 72, 71, 67, 121, 122, 86, 81, 116, 106, 104, 117, 78, 108, 118, 49, 48, 65, 77, 69, 53, 114, 43, 120, 104, 89, 120, 50, 98, 80, 66, 50, 77, 87, 80, 103, 110, 117, 118, 118, 97, 78, 106] </p>
  
  <p>encrypted text : a/MOvoOF/WZCQb3JSXaDTiHGCyzVQtjhuNlv10AME5r+xhYx2bPB2MWPgnuvvaNj </p>
  
  <p>Decrypt text.getbytes : [97, 47, 77, 79, 118, 111, 79, 70, 47, 87, 90, 67, 81, 98, 51, 74, 83, 88, 97, 68, 84, 105, 72, 71, 67, 121, 122, 86, 81, 116, 106, 104, 117, 78, 108, 118, 49, 48, 65, 77, 69, 53, 114, 43, 120, 104, 89, 120, 50, 98, 80, 66, 50, 77, 87, 80, 103, 110, 117, 118, 118, 97, 78, 106] </p>
  
  <p>Decoded  Decryptted text : [107, -13, 14, -66, -125, -123, -3, 102, 66, 65, -67, -55, 73, 118, -125, 78, 33, -58, 11, 44, -43, 66, -40, -31, -72, -39, 111, -41, 64, 12, 19, -102, -2, -58, 22, 49, -39, -77, -63, -40, -59, -113, -126, 123, -81, -67, -93, 99] </p>
  
  <p>Swift Output: 
  encryptedText   :  a/MOvoOF/WZCQb3JSXaDTiHGCyzVQtjhuNlv10AME5r+xhYx2bPB2MWPgnuvvaNj</p>
  
  <p>decryptedText :  ۽瑒왿᪰߆牷ྐྵ䐫徺ꋴ鐧ݐ斑ﷃ翴㦦જ㤉ꄕ䞴櫘勐鼍ᐏ┓ീ學䥏㿖칵鬥솽ᢼ铡鴷⤃ꗞ䛂䋗쿠蒻⯨䍊䂷篥럟⤫俷違둘๔Ꞵ‵</p>
  
  <p>Swift and java encryption matches.</p>
  
  <p>Any help is much appreciated.</p>