加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – Decrypted String总是返回Null

发布时间:2020-12-14 19:38:22 所属栏目:百科 来源:网络整理
导读:使用从服务器获取的数据 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=
使用从服务器获取的数据

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,以下代码也会返回
可选的(””)

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

为什么会这样?请指教.

解决方法

Per MartinR的评论:

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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读