如何使用 Xcode 在 Swift 中对字符串进行 AES 128 加密并将其作为 POST 发送到服务器?... 我是 Xcode 的新手,正在学习加密字符串数据并想发送到 HTTP 服务器。这是一个基本的 iOS 应用程序,用于发送设备的纬度和经度。
最佳答案
基于以下示例:https://github.com/krzyzanowskim/CryptoSwift
使用 CryptoSwift 加密字符串:
func encrypt(text: String) -> String? {
if let aes = try? AES(key: "passwordpassword", iv: "drowssapdrowssap"),
let encrypted = try? aes.encrypt(Array(text.utf8)) {
return encrypted.toHexString()
}
return nil
}
解密:
func decrypt(hexString: String) -> String? {
if let aes = try? AES(key: "passwordpassword", iv: "drowssapdrowssap"),
let decrypted = try? aes.decrypt(Array<UInt8>(hex: hexString)) {
return String(data: Data(bytes: decrypted), encoding: .utf8)
}
return nil
}
要将值发送到服务器查找: HTTP Request in Swift with POST method或关于如何将数据发送到服务器的数千篇文章中的任何一篇。
关于ios - 如何使用 Xcode 在 Swift 中对字符串进行 AES 128 加密并将其作为 POST 发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46469164/