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

后端 未结 2 1028
广开言路
广开言路 2021-01-25 10:58

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

2条回答
  •  Happy的楠姐
    2021-01-25 11:06

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

    To encrypt a string using 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(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 method or any one of the thousands of posts how to send data to server.

提交回复
热议问题