ios - 崩溃 : AES128 encryption in iOS 10 using Swift 2. 1

标签 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(..) 函数上崩溃,原因是 fatal error :在展开可选值时意外发现 nil

我已经交叉验证消息和 key 没有问题。

如有任何帮助,我们将不胜感激!

最佳答案

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

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

到:

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

关于ios - 崩溃 : AES128 encryption in iOS 10 using Swift 2. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278194/

相关文章:

ios - SKStoreReviewController requestReview 方法需要时间来显示并且应用程序卡住,直到出现 ReviewController

ios - 在动画完成之前更改 CALayer 内容

ios - 在 iOS 的条形图中显示整数值标签

ios - Xcode ios 限制 UIAlertView Textview 中的字符

xcode - Firebase Crashlytics : Missing dsym files

java - 如何在 java 中实现委托(delegate)模式(如在 objective-c 中)

objective-c - 谷歌广告转化片段 : Obj-C to Swift

ios - 如何使用 ARKit ARSKViewDelegate 渲染 jpg 图像?

ios - 从alamofire获取数据(responseString)

xcode - 如何获得 iOS 16 for Simulator?