Comments (8)
Instead of using these methods to serialize, I'm using buffer.toString('base64')
and new Buffer(base64Text,'base64)
from aes-js.
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.
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.
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.
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.
Yeah, or maybe enforcing an encoding is passed in and not just defaulting to 'utf8'...
Thanks for feedback. :)
from aes-js.
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.
I just did it. I used the Buffer key instead of the intArray one.
from aes-js.
Related Issues (20)
- Big endian HOT 2
- Is this still maintained? HOT 2
- Transform key and iv bytes in base64/Hex for another languages decript HOT 1
- export an es module HOT 2
- CBC encrypt and decrypt is wrong on output HOT 3
- CTR mode: Counter increment issue with 2 different IVs HOT 1
- Uncaught Error: invalid plaintext size (must be 16 bytes) HOT 3
- Recommending CTR is dangerous, if the same key is used twice HOT 7
- Encrypt CBC padding 7 is different from Java HOT 6
- Please add index.min.js
- need php/openssl advice HOT 1
- Make note of `yarn add -D @types/aes-js` in README HOT 1
- Error: invalid plaintext size (must be multiple of 16 bytes) HOT 6
- version 2.1.1 , ECB
- Incorrect UTF-8 decoding HOT 2
- Getting Chinese letters
- Should provide method to generate IV HOT 2
- Feature Request: Import key from hex HOT 1
- how do I encrypt images with this module
- How to convert a string to key? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aes-js.