.Net Code :
public string AESEncrypt(string clearText,string key) { string EncryptionKey = key; // "MAKV2SPBNI99212"; byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { int iterations = 1024; Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }, iterations); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()); } } return clearText; } Swift Code :
// "MAKV2SPBNI99212" Key func AESEncryptedString( withKey key : String) -> String? { let salt : Array <UInt8> = [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76] let saltData = NSData(bytes: salt, length: 13) let myPassData : NSData = key.data(using: String.Encoding.utf8)! as NSData var key = [UInt8](repeating: 0, count: 48) var initialVector = [UInt8](repeating: 0, count: 16) let ptrData = myPassData.bytes.assumingMemoryBound(to: Int8.self) let ptrSalt = saltData.bytes.assumingMemoryBound(to: UInt8.self) let keyPtr = UnsafeMutablePointer<UInt8>(mutating: key) CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), ptrData, myPassData.length, ptrSalt, saltData.length, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1), 1024, keyPtr, 48) initialVector = Array(key[32..<48]) key = Array(key[0..<32]) let keyD = Data(bytes: key) let ivD = Data(bytes:initialVector) let rawData = self.data(using: String.Encoding.unicode) let encryptedData = testCrypt(data:rawData!, keyData:keyD, ivData:ivD, operation:kCCEncrypt) let decryptedData = testDeCrypt(data:encryptedData, keyData:keyD, ivData:ivD, operation:kCCDecrypt) let decrypted = String(bytes:decryptedData, encoding:String.Encoding.utf16)! print("Encrypted Data: \(encryptedData.base64EncodedString()) \n with count: \(encryptedData.base64EncodedString().characters.count)") print("Decrypted: \(decrypted)") let encryptedString = encryptedData.base64EncodedString() return encryptedString } func testCrypt(data:Data, keyData:Data, ivData:Data, operation:Int) -> Data { let buffer_size : size_t = (data as NSData).length + kCCBlockSizeAES128 let buffer = UnsafeMutablePointer<NSData>.allocate(capacity: buffer_size) var num_bytes_encrypted : size_t = 0 let operation = CCOperation(operation) let algoritm = CCAlgorithm(kCCAlgorithmAES) let options = CCOptions( kCCOptionPKCS7Padding) let keyLength = size_t(kCCKeySizeAES256) let Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, (keyData as NSData).bytes, keyLength, (ivData as NSData).bytes, (data as NSData).bytes, (data as NSData).length, buffer, buffer_size, &num_bytes_encrypted) if UInt32(Crypto_status) == UInt32(kCCSuccess){ let myResult: NSData = NSData(bytes: buffer, length: num_bytes_encrypted) free(buffer) let keyData: NSData = myResult let hexString = (keyData as Data).base64EncodedString() print(hexString) return myResult as Data }else{ free(buffer) return Data() } } func testDeCrypt(data:Data, keyData:Data, ivData:Data, operation:Int) -> Data { let decryptedData = NSMutableData(length: (data as NSData).length) var num_bytes_decrypted: size_t = 0 let operation = CCOperation(operation) let algoritm = CCAlgorithm(kCCAlgorithmAES) let options = CCOptions(kCCOptionPKCS7Padding) let keyLength = size_t(kCCKeySizeAES256) let Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, (keyData as NSData).bytes, keyLength, (ivData as NSData).bytes, (data as NSData).bytes, (data as NSData).length, decryptedData?.mutableBytes, (decryptedData?.length)!, &num_bytes_decrypted) if UInt32(Crypto_status) == UInt32(kCCSuccess){ decryptedData?.length = num_bytes_decrypted return decryptedData! as Data } else { return Data() } } We are able to Encrypt and Decrypt data in Swift but Encrypted data of .Net code is not getting decrypted in Swift.Please let me know if anything wrong with the code.We tried decrypting Encrypted data of swift in .net its working fine but we are not able to decrypt the .net data. please help us with that. I have tried almost every solution available.