Tag mismatch on java decrypt swift encryption when use key not utf8 #1036
Replies: 1 comment
-
cryptography has nothing to do with characters encoding itself. Keys is a series of bytes. As long as you provide the same bytes, it all match. What you need to do to make sure you convert a string to bytes in the same way on both ends. so you need to adjust either your Swift code or Java code to use the same thing in README there's an example how to transform a string to array of bytes, and Swift provide convenient API for utf8, and not necessary for 8859-1. let password: Array<UInt8> = Array("s33krit".utf8) if you want to use Swift, you should go with something along the lines (I didn't test it, just guessing) let password = Array<UInt8>("password".data(using: .isoLatin1)!) |
Beta Was this translation helpful? Give feedback.
-
i am use swift code var key = try CryptoSwift.PKCS5.PBKDF2(
password: password,
salt: salt,
iterations: 4096,
keyLength: 32, /* AES-256 */
variant: .sha2(SHA2.Variant.sha256)
).calculate()
let aes = try AES(key: key, blockMode: GCM(iv: iv, mode: .combined), padding: .noPadding)
let inputData = Array("Hello world".utf8)
let encryptedBytes = try aes.encrypt(inputData)
My server in java use AES/GCM/NoPadding and
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec((new String(keyValue, StandardCharsets.ISO_8859_1)).toCharArray(), salt, 4096, 256);
if key value in utf8 swift and java code work normally, but we use keyValue byte array ISO_8859_1 in this case, Java cannot decode the encoded data in Swift javax.crypto.AEADBadTagException: Tag mismatch!
Beta Was this translation helpful? Give feedback.
All reactions