为什么这款AES128 Decryption在iPhone上需要这么长时间?

时间:2015-03-16 14:30:25

标签: ios objective-c swift encryption nsdata

我正在解密从php服务器发送的图像。 我使用CryptoSwift库来解密图像,图像以NSData形式出现,在解密之后,我从NSData创建了一个UIImage。

但是解密需要大约1分钟和10秒,这非常慢。 图像数据的大小:

println(imageData.length)
result: 32592

我相信这不是一个大文件吗? 这是我用来解密图像数据的代码:

let aes = AES(key: keyData, iv: ivData, blockMode: .ECB) 
let decryptedData = aes?.decrypt(encryptedSnap, removePadding: true)
let image = UIImage(data: decryptedData!)

我尝试在不同的线程上运行该进程,但它给出了相同的结果。 当我解密图像时,模拟器使用100%的CPU,大约21.5MB的RAM。

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

Swift 2.0

以下是一些使用ECB模式和128位AES密钥加密和解密NSData的简单示例代码。

测试代码

let keyString = "M02cnQ51Ji97vwT4"
let keyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

let message = "Don´t try to read this text. Top Secret Stuff"
let data = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

print("data: \(data)")
if let encryptedData = testCrypt(data, keyData:keyData, operation:UInt32(kCCEncrypt)) {
    print("encryptedData: \(encryptedData)")
    if let decryptedData = testCrypt(encryptedData, keyData:keyData, operation:UInt32(kCCDecrypt)) {
        print("decryptedData: \(decryptedData)")
    }
}

加密方法:

func testCrypt(data:NSData, keyData:NSData, operation:CCOperation) -> NSData? {
    let keyBytes = UnsafePointer<UInt8>(keyData.bytes)
    print("keyLength   = \(keyData.length), keyData   = \(keyData)")

    let dataLength = Int(data.length)
    let dataBytes  = UnsafePointer<UInt8>(data.bytes)
    print("dataLength  = \(dataLength), data      = \(data)")

    let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
    let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData.mutableBytes)
    let cryptLength  = size_t(cryptData.length)

    let keyLength              = size_t(kCCKeySizeAES128)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
    let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(operation,
        algoritm,
        options,
        keyBytes, keyLength,
        nil,
        dataBytes, dataLength,
        cryptPointer, cryptLength,
        &numBytesEncrypted)

    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        cryptData.length = Int(numBytesEncrypted)
        print("cryptLength = \(numBytesEncrypted), cryptData = \(cryptData)")

    } else {
        print("Error: \(cryptStatus)")
    }

    return cryptData;
}

输出:

data:                         <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>
keyLength   = 16, keyData   = <4d303263 6e513531 4a693937 76775434>
dataLength  = 46, data      = <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>

cryptLength = 48, cryptData = <5fd86c65 6544720c 9659b43f 2e77bf8d 9c2373d9 e1042a3d ce9a19f8 2900521e c3f8075a b6866ba5 2fcd5793 bbeb8e0c>
encryptedData:                <5fd86c65 6544720c 9659b43f 2e77bf8d 9c2373d9 e1042a3d ce9a19f8 2900521e c3f8075a b6866ba5 2fcd5793 bbeb8e0c>
keyLength   = 16, keyData   = <4d303263 6e513531 4a693937 76775434>
dataLength  = 48, data      = <5fd86c65 6544720c 9659b43f 2e77bf8d 9c2373d9 e1042a3d ce9a19f8 2900521e c3f8075a b6866ba5 2fcd5793 bbeb8e0c>

cryptLength = 46, cryptData = <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>
decryptedData:                <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>