AES encryption and decompression do not work in node.js

Asked 2 years ago, Updated 2 years ago, 48 views

What do you want to do?

Compression → AES cipher → Hand it over to the other party → AES decryption → Unzip

That's it.

Executing the code below will result in an "incorrect header check" error on the last line.

There is already a difference between the return value of zlib.deflateSync and the dec passed to zlib.inflateSync(), so of course, but I don't know how to describe AES encryption.
If you have any details, please take care of me.

By the way, regardless of compression, we have confirmed that the logic of AES encryption and decryption itself is correct.On the other hand, regardless of encryption, we make sure that just compression and decompression work properly.If you combine the two, the format (type?) will not work.

(I use let instead of var because it is a typescript)

let password='password';

letm = crypto.createHash('sha256');
m.update(password);
let crypto_key_hash = m.digest('binary');

// ZLIB compression
let data = 'this is request ticket.';
data=zlib.deflateSync(data);


// AES encryption
let cipher=cryptor.createCipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
letencData=cipher.update(data, 'binary', 'base64');
encData+=cipher.final('base64');

// HTTP communication to the other party...

// AES decoding
letdecipher=cryptor.createDecipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
let dec = decipher.update(encData, 'base64', 'binary');
dec+=decipher.final('binary');

dec = new Buffer(dec);

// ZLIB thawing
dec = zlib.inflateSync(dec);

Note:
I realize that the problem is that the return value of deflateSync does not match the binary encoding.
I succeeded when I used the Buffer class to convert it to BASE64 and the decryption output to BASE64.

let password='password';

letm = crypto.createHash('sha256');
m.update(password);
let crypto_key_hash = m.digest('binary');

// ZLIB compression
let data = 'this is request ticket.';
data=zlib.deflateSync(data);

data = new Buffer(data).toString('base64');


// AES encryption
let cipher=cryptor.createCipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
letencData=cipher.update(data, 'base64', 'base64');
encData+=cipher.final('base64');

// HTTP communication to the other party...

// AES decoding
letdecipher=cryptor.createDecipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
let dec = decipher.update(encData, 'base64', 'base64');
dec+=decipher.final('base64');

dec = new Buffer(dec, 'base64');

// ZLIB thawing
dec = zlib.inflateSync(dec);

dec = new Buffer(dec).toString('utf8');

But they seem to be doing something inefficient.
Is there any way to improve the format?

node.js

2022-09-29 22:38

1 Answers

I solved myself.
Now that I know that cipher.update can handle Buffer directly, I wrote the following:

let password='password';

letm = crypto.createHash('sha256');
m.update(password);
let crypto_key_hash = m.digest('binary');

// ZLIB compression
let data = 'this is request ticket.';
data=zlib.deflateSync(data);

// AES encryption
let cipher=cryptor.createCipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
letencData=cipher.update(data, '', 'hex');
encData+=cipher.final('hex');

// HTTP communication to the other party...

// AES decoding
letdecipher=cryptor.createDecipheriv('aes-256-cbc', crypto_key_hash, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
let dec = decipher.update(encData, 'hex');
dec=Buffer.concat([dec,decipher.final()]);

// ZLIB thawing
dec=zlib.inflateSync(dec).toString('utf8');


2022-09-29 22:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.