admin管理员组

文章数量:1401595

I create a cipher passing two buffers. buf1 is they key, a 32 bytes buffer, and buf2, which is the IV, is also a 32 bytes buffer that I slice to only use 16 bytes. Documentation says that cipher.update and cipher.final return a buffer when nothing is specified. That is actually what I wish to happen. While I guess I can just do a new Buffer(crypted, 'binary') to convert it, I'm wondering if I might be doing something wrong.

> var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
undefined
> var crypted = cipher.update(new Buffer('this is some test'));
undefined
> crypted += cipher.final();
'!t\u001f\u0004>.\u0012\u0001���K\u001bSiA�]3\u0017�6�&�.��\u0015�V?'
0> Buffer.isBuffer(crypted)
false

.html#crypto_class_cipher

I'm using node.js version 0.10.10, which is latest stable and supposedly matches the documentation linked:

$ node -v
v0.10.10

Is this a documentation bug or a mistake from my part? I know that with v0.8 pbkdf2 returned a binary string instead of a buffer and now with 0.10.10 it returns a buffer as stated by the docs. I was hoping for cipher to also work it all with buffers instead of using binary... for consistency.

I create a cipher passing two buffers. buf1 is they key, a 32 bytes buffer, and buf2, which is the IV, is also a 32 bytes buffer that I slice to only use 16 bytes. Documentation says that cipher.update and cipher.final return a buffer when nothing is specified. That is actually what I wish to happen. While I guess I can just do a new Buffer(crypted, 'binary') to convert it, I'm wondering if I might be doing something wrong.

> var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
undefined
> var crypted = cipher.update(new Buffer('this is some test'));
undefined
> crypted += cipher.final();
'!t\u001f\u0004>.\u0012\u0001���K\u001bSiA�]3\u0017�6�&�.��\u0015�V?'
0> Buffer.isBuffer(crypted)
false

http://nodejs/api/crypto.html#crypto_class_cipher

I'm using node.js version 0.10.10, which is latest stable and supposedly matches the documentation linked:

$ node -v
v0.10.10

Is this a documentation bug or a mistake from my part? I know that with v0.8 pbkdf2 returned a binary string instead of a buffer and now with 0.10.10 it returns a buffer as stated by the docs. I was hoping for cipher to also work it all with buffers instead of using binary... for consistency.

Share Improve this question asked Jun 7, 2013 at 18:48 MamsaacMamsaac 6,2733 gold badges23 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You're concatenating them using += which turns buffers into strings. Your code should be something more like this :

var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
var cryptedBuffers = [cipher.update(new Buffer('this is some test'))];
cryptedBuffers.push(cipher.final());

// at this point `cryptedBuffers` is an array of buffers which you can turn 
// into a single buffer by doing

var crypted = Buffer.concat(cryptedBuffers);

本文标签: javascriptcryptocreateCipheriv gt cipherupdatecipherfinal does not return a BufferStack Overflow