Imanuel Pardosi Imanuel Pardosi - 1 year ago 62
iOS Question

AES Encryption to strange characters

I am using AES Encryption in swift 3, I use this CryptoSwift library.
This is my code to encrypt a string and the result is readable string: /QOEtrf3o8buv2wA9FeAyg==. How can I get the strange character (non readable) like this: Ί�^��h��y^ғ.

var input = "CryptoSwift"
var key = "passwordpassword"
var iv = "drowssapdrowssap"

func aesEncrypt(input: String, key: String, iv: String) throws -> String {
let data = input.utf8
let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data))
let encryptedData = Data(encrypted)
return encryptedData.base64EncodedString()
}

let encrypted = try! aesEncrypt(input: input, key: key, iv: iv)
print("encrypted: \(encrypted)")



---------

Result: /QOEtrf3o8buv2wA9FeAyg==

I want the result something like this: Ί�^��h��y^ғ, the strange characters.


Answer Source

You shouldn't convert it to Base64.

        let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data))
        let encryptedData = Data(bytes: UnsafePointer<UInt8>(encrypted), count: Int(encrypted.count))
        // let encryptedString = String(data: encryptedData,encoding: String.Encoding.utf8)
        // use the encryptedData to write it into a file.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download