ios - 如何使用 Xcode 在 Swift 中对字符串进行 AES 128 加密并将其作为 POST 发送到服务器?

标签 ios swift xcode encryption aes

如何使用 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/

相关文章:

ios - 单击按钮移动到 iOS 中的另一个 View Controller

ios - 在当前应用中添加 UIPageViewController

ios - keytoolbar 上的下一个按钮将最后一个字段设置为第一响应者,

ios - 如何快速隐藏customView中的按钮

ios - 如何快速双击(而不是单击) Collection View 单元格?

iphone - 我如何为这个应用程序编写逻辑

ios - 有没有办法检测 iPhone 是否处于反色模式?

ios - 在 Xcode 模拟器中将 View 堆栈压缩到一个 block 中

swift - 通过 Kinvey 使用对象映射

iphone - 构建失败,xcode 4.4中没有错误