解密的字符串始终返回Null [英] Decrypted String always returning Null

查看:79
本文介绍了解密的字符串始终返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下方式从服务器获取的数据

Data fetched from server using

var request = var request = URLRequest(url: URL(string: "http://www.example.com/test.php")!)   
        request.httpMethod = "POST"   
        let akey:String =  txt_key.stringValue;   
        let email:String = txt_email.stringValue   
        let VAL:String="test"      
        var data="blah"   
        let postString = data   
        request.httpBody = postString.data(using: .utf8)   
        let task = URLSession.shared.dataTask(with: request) { data, response, error in   
            guard let data = data, error == nil else {                                                 /   
                print("error=\(error)")   
                return   
            }   
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           /   
                print("statusCode should be 200, but is \(httpStatus.statusCode)")   
                print("response = \(response)")   
            }   
            let responseString = String(data: data, encoding:  .utf8)   
            print(responseString)   
        }   
        task.resume()   

使用解密

   func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
        if let keyData = key.data(using: String.Encoding.utf8),
            let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
            let cryptData    = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128) {

            let keyLength              = size_t(kCCKeySizeAES128)
            let operation: CCOperation = UInt32(kCCDecrypt)
            let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
            let options:   CCOptions   = UInt32(options)

            var numBytesEncrypted :size_t = 0

            let cryptStatus = CCCrypt(operation,
                                      algoritm,
                                      options,
                                      (keyData as NSData).bytes, keyLength,
                                      iv,
                                      data.bytes, data.length,
                                      cryptData.mutableBytes, cryptData.length,
                                      &numBytesEncrypted)

            if UInt32(cryptStatus) == UInt32(kCCSuccess) {
                cryptData.length = Int(numBytesEncrypted)
                let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
                return unencryptedMessage
            }
            else {
                return nil
            }
        }
        return nil
}

即使 responseString 不为null,以下代码也总是返回 optional(")

Even when responseString is not null,The following code always returns optional("")

 let unencode = String(describing: responseString!).aesDecrypt(key: "123456789012asdsadasd", iv: "iv-salt-string--")

为什么会这样?请指教.

Why is this happening ? Please advice.

推荐答案

每个MartinR的评论:

Per MartinR's comment:

我已验证"JDoNBXk21oJ9x15Bkv12uw =="正是使用您的密钥和iv加密空字符串的结果.错误出现在您的PHP脚本中,而不是Swift代码中.

I have verified that "JDoNBXk21oJ9x15Bkv12uw==" is exactly the result of encrypting an empty string with your key and iv. The error is in your PHP script, not in the Swift code.

因此,您需要解决服务器上的错误.

Therefore you need to resolve the error on the server.

这篇关于解密的字符串始终返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆