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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46469164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:20:15  来源:igfitidea点击:

How to do AES 128 encryption of a string in Swift using Xcode and send it as POST to the server?

iosswiftxcodeencryptionaes

提问by M J A

How to do AES 128 encryption of a string in Swift using Xcode and send it as POST to the server?... I am new to Xcode and am learning to encrypt the string data and want to send to HTTP server. It is a basic iOS app for sending Latitude and Longitude of the device.

如何使用 Xcode 在 Swift 中对字符串进行 AES 128 加密并将其作为 POST 发送到服务器?...我是 Xcode 的新手,正在学习加密字符串数据并想发送到 HTTP 服务器。它是一个基本的 iOS 应用程序,用于发送设备的纬度和经度。

回答by Au Ris

Based on examples from: https://github.com/krzyzanowskim/CryptoSwift

基于以下示例:https: //github.com/krzyzanowskim/CryptoSwift

To encrypt a string using 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
}

To decrypt:

解密:

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
}

To send the values to server look up: HTTP Request in Swift with POST methodor any one of the thousands of posts how to send data to server.

要将值发送到服务器查找: 使用 POST 方法的 Swift 中的 HTTP 请求或如何将数据发送到服务器的数千个帖子中的任何一个。

回答by Brandon

Add #import <CommonCrypto/CommonCryptor.h>to your Bridging Header then use the following:

添加#import <CommonCrypto/CommonCryptor.h>到您的桥接头,然后使用以下内容:

func encryptAES128(data: NSData, key: NSString, iv: NSString) -> NSData? {
    let keyPtr = UnsafeMutablePointer<CChar>.allocate(capacity: Int(kCCKeySizeAES128) + 1)

    defer {
        keyPtr.deallocate(capacity: Int(kCCKeySizeAES128) + 1)
    }

    let ivPtr = iv.utf8String
    bzero(keyPtr, 0)
    key.getCString(keyPtr, maxLength: Int(kCCKeySizeAES128) + 1, encoding: String.Encoding.utf8.rawValue)

    let bufferSize = data.length + kCCBlockSizeAES128
    let buffer = UnsafeMutableRawPointer.allocate(bytes: bufferSize, alignedTo: MemoryLayout.alignment(ofValue: CChar.self))

    var numBytesEncrypted = 0

    let cryptStatus = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), CCOptions(kCCOptionPKCS7Padding), keyPtr, kCCKeySizeAES128, ivPtr, data.bytes, data.length, buffer, bufferSize, &numBytesEncrypted)

    if cryptStatus == kCCSuccess {
        return NSData(bytesNoCopy: buffer, length: numBytesEncrypted, freeWhenDone: true)
    }

    buffer.deallocate(bytes: bufferSize, alignedTo: MemoryLayout.alignment(ofValue: CChar.self))
    return nil
}

To send it to the server, base64-encode the result, and send that. Then on the server side, base64-decode the result and decrypt it.

要将其发送到服务器,对结果进行 base64 编码,然后发送。然后在服务器端,对结果进行 base64 解码并解密。