Having trouble decrypting a string using an encryption key and an IV.
var key: String
var iv: String
func decryptData(_ encryptedText: String) -> String?
{
if let textData = Data(base64Encoded: iv + encryptedText) {
do {
let sealedBox = try AES.GCM.SealedBox(combined: textData)
let key = SymmetricKey(data: key.data(using: .utf8)!)
let decryptedData = try AES.GCM.open(sealedBox, using: key)
return String(data: decryptedData, encoding: .utf8)
} catch {
print("Decryption failed: \(error)")
return nil
}
}
return nil
}
Proper coding choices aside (I'm just trying anything at this point,) the main problem is opening the SealedBox. If I go to an online decryption site, I can paste in my encrypted text, the encryption key, and the IV as plain text and I can encrypt and decrypt just fine.
But I can't seem to get the right combo in my Swift code. I don't have a "tag" even though I'm using the combined option. How can I make this work when all I will be receiving is the encrypted text, the encryption key, and the IV. (the encryption key is 256 bits)
Try an AES site with a key of 32 digits and an IV of 16 digits and text of your choice. Use the encrypted version of the text and then the key and IV in my code and you'll see the problem. I can make the SealedBox but I can't open it to get the decrypted data. So I'm not combining the right things the right way. Anyone notice the problem?