Closed AlexMesser closed 3 years ago
Found a solution.
We must initialize LazySodium with ISO_8859_1
charset:
val lazySodium = LazySodiumAndroid(SodiumAndroid(), Charsets.ISO_8859_1)
then we must use the same charset for plain string methods
val cek = lazySodium.keygen(AEAD.Method.XCHACHA20_POLY1305_IETF)
val plainStringCek = cek.getAsPlainString(Charsets.ISO_8859_1)
val restoredCek = Key.fromPlainString(plainStringCek, Charsets.ISO_8859_1)
Using asPlainString
and fromPlainString
is pretty dangerous because it uses UTF-8 as an encoding. Generally you should encode using Hex (we have these in LS as Key.asHexString
and Key.fromHexString
) or Base64.
Converting raw bytes to a UTF-8 string can lead to inconsistent outputs as you have found out. In your case you can use:
val cek = lazySodium.keygen(AEAD.Method.XCHACHA20_POLY1305_IETF)
val hexStringCek = cek.asHexString
val restoredCek = Key.fromHexString(hexStringCek)
but if I restore key using HEX - data will be successfully decrypted:
Also keys restored from hex and from bytes are different from key restored from plain string: