I'm trying to encrypt a String using a password in Swift but not sure how to do it. I need something that works like this
let password = "password"
let message = "messageToEncrypt"
let encryptedMessage = encrypt(message, password)
...
let decryptedMessage = decrypt(encryptedMessage, password)
Any advice would be much appreciated.
Thanks
UPDATE
Based on the idea below i have the following method
func testEnc() throws {
let ivKey = "tEi1H3E1aj26XNro"
let message = "Test Message"
let password = "pass123"
let aesKey = password.padding(toLength: 32, withPad: "0", startingAt: 0)
let aes = try AES(key: aesKey, iv: ivKey)
let cipherBytes: Array<UInt8> = try aes.encrypt(Array(message.utf8))
let cipherData = NSData(bytes: cipherBytes, length: Int(cipherBytes.count))
let cipherString = cipherData.base64EncodedString(options: .lineLength64Characters)
//cipherString => beQ7u8hBGdFYqNP5z4gBGg==
let decryptedCipherBytes = try aes.decrypt(Array(cipherString.utf8))
let decryptedCipherData = NSData(bytes: decryptedCipherBytes, length: Int(cipherBytes.count))
let decryptedCipherString = decryptedCipherData.base64EncodedString(options: .lineLength64Characters)
assert(message == decryptedCipherString)
}
at the line
let decryptedCipherBytes = try aes.decrypt(Array(cipherString.utf8))
I am getting the following error:
[CryptoSwift.AES.Error: dataPaddingRequired]
Conform 'CryptoSwift.AES.Error' to Debugging.Debuggable to provide more debug information.
Do you have any idea why it would not be able to decrypt the data that it has just encrypted?
Thanks