How to do AES 128 encryption of a string in Swift using Xcode and send it as POST to the server?... I am new to Xcode and am learning to encrypt the string data and want to send to HTTP server. It is a basic iOS app for sending Latitude and Longitude of the device.
Asked
Active
Viewed 1.4k times
2 Answers
12
Based on examples from: https://github.com/krzyzanowskim/CryptoSwift
To encrypt a string using CryptoSwift:
func encrypt(text: String) -> String? {
if let aes = try? AES(key: "passwordpassword", iv: "drowssapdrowssap"),
let encrypted = try? aes.encrypt(Array(text.utf8)) {
return encrypted.toHexString()
}
return nil
}
To decrypt:
func decrypt(hexString: String) -> String? {
if let aes = try? AES(key: "passwordpassword", iv: "drowssapdrowssap"),
let decrypted = try? aes.decrypt(Array<UInt8>(hex: hexString)) {
return String(data: Data(bytes: decrypted), encoding: .utf8)
}
return nil
}
To send the values to server look up: HTTP Request in Swift with POST method or any one of the thousands of posts how to send data to server.

Au Ris
- 4,541
- 2
- 26
- 53
-
1I tried the CryptoSwift but while building, it shows 32 errors in degarding Cryptoswift files – M J A Sep 29 '17 at 14:18
-
You maybe using CryptoSwift version which is not supported by your current XCode/Swift version. The last CryptoSwift version that supports swift 3 is 0.6.9 as far as I know. Which Xcode version are you using? – Au Ris Sep 29 '17 at 14:30
-
Also, I wanted to know, would the data be in binary form after encryption. – M J A Oct 03 '17 at 07:40
-
And which version of CryptoSwift are you using? The latest is 0.7.2 and it should work with Swift 4 and Xcode 9. Regarding your latest question, the encrypt function I posted returns an optional String, and String is probably what you want to use for sending it over the network. – Au Ris Oct 03 '17 at 08:44
-
-
CryptoSwift AES encrypt method returns Array
, which is an array of bytes where each byte is an unsigned integer ranging from 0 to 255, thus 8 bit representation. In my example I convert the array to a hex string and return that, because that's the most common way to share encrypted data. If you want as you said earlier a binary representation, you could loop through the array, convert each array element (UInt8) to a binary string and concatenate them into one string of 0's and 1's. – Au Ris Oct 03 '17 at 12:43 -
4
Add #import <CommonCrypto/CommonCryptor.h>
to your Bridging Header then use the following:
func encryptAES128(data: NSData, key: NSString, iv: NSString) -> NSData? {
let keyPtr = UnsafeMutablePointer<CChar>.allocate(capacity: Int(kCCKeySizeAES128) + 1)
defer {
keyPtr.deallocate(capacity: Int(kCCKeySizeAES128) + 1)
}
let ivPtr = iv.utf8String
bzero(keyPtr, 0)
key.getCString(keyPtr, maxLength: Int(kCCKeySizeAES128) + 1, encoding: String.Encoding.utf8.rawValue)
let bufferSize = data.length + kCCBlockSizeAES128
let buffer = UnsafeMutableRawPointer.allocate(bytes: bufferSize, alignedTo: MemoryLayout.alignment(ofValue: CChar.self))
var numBytesEncrypted = 0
let cryptStatus = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), CCOptions(kCCOptionPKCS7Padding), keyPtr, kCCKeySizeAES128, ivPtr, data.bytes, data.length, buffer, bufferSize, &numBytesEncrypted)
if cryptStatus == kCCSuccess {
return NSData(bytesNoCopy: buffer, length: numBytesEncrypted, freeWhenDone: true)
}
buffer.deallocate(bytes: bufferSize, alignedTo: MemoryLayout.alignment(ofValue: CChar.self))
return nil
}
To send it to the server, base64-encode the result, and send that. Then on the server side, base64-decode the result and decrypt it.

Brandon
- 22,723
- 11
- 93
- 186
-
-
Sorry, if you thought this way. I am new to swift and while learning online, I always got confused because some websites wrote codes without mentioning if its Swift or Obj C. Just wanted to ask to confirm. – M J A Oct 03 '17 at 07:38