崩溃:使用Swift 2.1在iOS 10中加密AES128

时间:2017-04-07 12:29:11

标签: ios swift xcode encryption aes

我正在使用 AES128加密。以下是我使用过的代码:

. . .
let encJson : String = response.objectForKey("data") as! String

let encryptionKey : String = “some key here”

//Crash on this line
response = EncryptionAndConversion.sharedInstance.converEncDataToStringWithAES128(encJson, key: encryptionKey)

print("response : \(response)")
. . .

func converEncDataToStringWithAES128(message:String, key myKey:String) -> NSDictionary {

        let data = NSData(base64EncodedString: message, options: NSDataBase64DecodingOptions(rawValue: 0))

        //The value of result comes nil
        let result = NSString(data: (data?.AES128DecryptedDataWithKey(myKey))!, encoding: NSUTF8StringEncoding)

        return convertStringToDictionary(result!)
    }

可能是什么原因?我的converEncDataToStringWithAES128(..)功能因原因而崩溃 致命错误:在解开可选值时意外发现nil

我已经交叉验证了邮件和密钥没有问题。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

不是答案,但是这也会崩溃吗?

let encJson : String = response.objectForKey("data") as! String

为:

guard let encJson = response.objectForKey("data") as? String {
    print("data is not a String")
    return
}