I want to encrypt and decrypt a text in Swift using an AES algorithm that should be compatible with Android and PHP encryption algorithms. So, I been trying the code from the question here, but the result prints nothing. This is the code with little modifications from the original:
var key = "abcdefghijklmnopqrstuvwxyz012345"
var iv = "0000000000000000"
var plainText = "Hello World"
let keyData: NSData! = (key as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let keyBytes = UnsafePointer<UInt8>(keyData.bytes)
let keyLength = size_t(kCCKeySizeAES256)
let plainData = (plainText as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let dataLength = size_t(plainData.length)
let dataBytes = UnsafePointer<UInt8>(plainData.bytes)
var bufferData = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
var bufferPointer = UnsafeMutablePointer<UInt8>(bufferData!.mutableBytes)
let bufferLength = size_t(bufferData!.length)
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options = UInt32(kCCOptionPKCS7Padding)
let ivData: NSData! = (iv as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let ivPointer = UnsafePointer<UInt8>(ivData.bytes)
var numBytesEncrypted: UnsafeMutablePointer<Int> = nil
var cryptStatus = CCCrypt(operation, algoritm, options, keyBytes, keyLength, ivPointer, dataBytes, dataLength, bufferPointer, bufferLength, numBytesEncrypted)
bufferData!.length = numBytesEncrypted.hashValue
let base64cryptString = bufferData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
print(base64cryptString)
Do I miss something? Please help! If you know how to solve it or got some code that works for AES encryption & decryption I'll appreciate it.