Reputation: 331
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.
Upvotes: 1
Views: 3760
Reputation: 433
Here is how I use it to properly decrypt and encrypt strings. You need to make string in hexString and hexString in data later. This is how CryptoSwift work.
extension String{
func aesEncrypt(key: String, iv: String) throws -> String {
let data = self.data(using: .utf8)!
let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC).encrypt([UInt8](data))
let encryptedData = Data(encrypted)
try? encryptedData.toHexString().aesDecrypt(key: key, iv: iv)
return encryptedData.toHexString()
}
func aesDecrypt(key: String, iv: String) throws -> String {
let data = self.dataFromHexadecimalString()!
print(data)
let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC).decrypt([UInt8](data))
let decryptedData = Data(decrypted)
return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}
func dataFromHexadecimalString() -> Data? {
var data = Data(capacity: characters.count / 2)
let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
regex.enumerateMatches(in: self, options: [], range: NSMakeRange(0, characters.count)) { match, flags, stop in
let byteString = (self as NSString).substring(with: match!.range)
var num = UInt8(byteString, radix: 16)
data.append(num!)
}
return data
}
}
extension Data {
public var bytes: Array<UInt8> {
return Array(self)
}
public func toHexString() -> String {
return self.bytes.toHexString()
}
}
Upvotes: 0
Reputation: 4615
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.
Upvotes: 2
Reputation: 299673
"Ί�^��h��y^ғ" isn't a proper string. The character "�" means "this isn't a character." (It's technical name is the "substitution character" that is used when a byte sequence is not valid for the Unicode-based encoding you're using). Since "�" could be many different byte sequences, "Ί�^��h��y^ғ" isn't really meaningful. There are a huge number of byte sequences that would decode into that nonsense string.
You're getting a "readable string" because you're encoding the random bytes that come out of the encryption function in Base64.
It's not really clear what you mean by "wanting" something that includes � (since that's nonsense). If you want the data, just return the Data
(don't call base64EncodedString()
). As a general rule, this is what you want to work with. Encrypted data is Data
, it's not `String.
What are you trying to do that you want a nonsense string that has lost information in the encoding?
Upvotes: 2
Reputation: 1
You're returning a base64 encoded string which will always be readable ASCII. I'm unfamiliar with CryptoSwift, but if you can find some way to return the raw encrypted data, it should look the way you want.
Upvotes: 0