Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect UTF-8 decoding #92

Open
swansontec opened this issue Jan 3, 2020 · 2 comments
Open

Incorrect UTF-8 decoding #92

swansontec opened this issue Jan 3, 2020 · 2 comments

Comments

@swansontec
Copy link

The utf8.fromBytes routine does not handle 4-byte character sequences.

Demo

$ echo -n 𠜎 | hexdump
0000000 f0 a0 9c 8e

The character 𠜎 has a 4-byte encoding, so let's try putting that into fromBytes:

const aesjs = require('aes-js')

const bytes = [0xf0, 0xa0, 0x9c, 0x8e]
const string = aesjs.utils.utf8.fromBytes(bytes)
console.log(string)

Nothing prints. Doing it with buffer works as expected:

console.log(Buffer.from(bytes).toString()) // Prints 𠜎
@ricmoo
Copy link
Owner

ricmoo commented Jan 3, 2020

Yes, I believe you are correct. I will be removing the UTF8 utilities in the next version of this library (soon, I hope) and recommending one of my other libraries for UTF8 coding, @ethersproject/strings (you can use the toUtf8Bytes and toUtf8String functions), which is far more robust.

I’ll pin this issue too, once I get to the coffee shop to work for the day. :)

Thanks!

@ricmoo ricmoo pinned this issue Jan 4, 2020
@roiconde
Copy link

roiconde commented Jan 7, 2020

I was able to fix this by just using TextEncoder / Decoder standard ( https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder ):

var textBytes = new TextEncoder().encode(text);
var decryptedText = new TextDecoder().decode(decryptedBytes);

There is also a polyfill in case you may want to add support for IE (linked from Mozilla):
https://github.com/inexorabletash/text-encoding

I would like to ask if you, as the developer of AESJS deem this kind of use safe for your script.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants