Giter Club home page Giter Club logo

Comments (8)

fresheneesz avatar fresheneesz commented on September 1, 2024

Instead of using these methods to serialize, I'm using buffer.toString('base64') and new Buffer(base64Text,'base64)

from aes-js.

ricmoo avatar ricmoo commented on September 1, 2024

Hey @fresheneesz,

Where did you get that series of bytes from? It doesn't seem to be a valid UTF-8 string. Not every series of bytes is a valid UTF-8 string. That function is meant to convert a valid UTF-8 string to bytes and vice versa.

There is certainly some more investigation I need to do, because this also fails to match those results:

> new Buffer((new Buffer([182,42,190,168])).toString(), 'utf8')
<Buffer ef bf bd 2a ef bf bd ef bf bd>

Are you passing this in as the key? You should also look at using a key-derivation function (such as crypt, crypt, scrypt, et cetera). Also, check out this section on normalizing the encoding: https://github.com/ricmoo/scrypt-js#encoding-notes

You should be able to just pass a buffer directly into aes as well...

from aes-js.

fresheneesz avatar fresheneesz commented on September 1, 2024

I got this error while encrypting data that contained RSA keys in standard format. After encrypting, I stored the result of convertBytesToString in localStorage, but when getting back from storage and using convertStringToBytes, it was mangled. I took just a little bit of the bytes array to make this minimal repro. It seems a bit problematic that a function like this would only work for data in a certain format - isn't this data supposed to be pretty arbitrary? In any case, I'm using them before encryption and after decryption without issue - it just seems a bit broken when used after encryption or before decryption.

from aes-js.

ricmoo avatar ricmoo commented on September 1, 2024

Oh, that will definitely fail then. :)

So, a String is a piece of data representing text in a specific encoding, in the case of encoding not being specified, it defaults to UTF-8 which has very specific rules. So, arbitrary binary data will quite often not follow these rules. See invalid sequences.

Also, UTF-8 encoding and decoding is not necessarily symmetric, decode(encode(data)) === data is not always true. The above notes on UTF normalization can give you some examples.

When encrypting and decrypting data, binary output is produced, which is why it is incompatible with UTF-8.

For your purposes, you may wish to store the string encoded as a hex string, by using:

var hexString = aes.util.convertBytesToString(bytes, "hex");
var original = aes.util.convertStringToBytes(hexString, "hex")

But base64 is fine too (I don't provide a base64 encoder/decoder in aes-js though), the important thing is these encodings (hex, base64, uuencode, et cetera) is that they are specifically designed to represent binary data using only printable ASCII bytes, while UTF-8 is not.

Here is more information on binary-to-text encodings: https://en.wikipedia.org/wiki/Binary-to-text_encoding

Hope that helps.

from aes-js.

fresheneesz avatar fresheneesz commented on September 1, 2024

Gotcha. Might be a good idea to rename these to something like convertUTFBytesToString, cause the name makes me think its more general.

from aes-js.

ricmoo avatar ricmoo commented on September 1, 2024

Yeah, or maybe enforcing an encoding is passed in and not just defaulting to 'utf8'...

Thanks for feedback. :)

from aes-js.

RicardoArauj0 avatar RicardoArauj0 commented on September 1, 2024

Hi how are you?
I'm trying to use aes-js on node:

var key = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'r', 'i', 'u', 'k', 'a', 'v']
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));

On the line 2 I receive the error: "Array contains invalid value 0,".
Do you know whats this could be?

I appreciate your help in advance.

from aes-js.

RicardoArauj0 avatar RicardoArauj0 commented on September 1, 2024

I just did it. I used the Buffer key instead of the intArray one.

from aes-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.