Discussion:
Trying to decrypt AES, bad decrypt
Thijs Koerselman
2013-06-27 13:57:53 UTC
Permalink
Hi,

I'm trying to encrypt some string in C#, send it to a Node server and
decrypt it there. I am having a hard time because I keep getting this error:

TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
decrypt
at Decipher.Cipher.final (crypto.js:257:27)

I have tried different AES modes and key sizes but it didn't help. Just for
testing I'm sending a json post to the server containing base64 encoded
versions of key, iv and the encrypted string (fingerprint in this example).

In the post request handler I do this:

var crypto = require('crypto');
var key = req.body.key;
var iv = req.body.iv;
var binkey = new Buffer(key, 'base64');
var biniv = new Buffer(iv, 'base64');

var crypted = req.body.fingerprint;
var bincrypted = new Buffer(crypted, 'base64');
var decipher = crypto.createDecipher('aes-256-cbc',binkey, biniv);
var dec = decipher.update(bincrypted,'binary','utf8');
dec += decipher.final('utf8');
console.log("dec", dec);

To be sure I'm using the same Aes mode and padding on the encryption this
is what's reported by my C# Aes, including the JSON being sent.

Keysize 256
Mode CBC
Padding PKCS7
Key: YJAG4xYTTQ0Ke3FBIDgmobERgbi/Tl/LYt9cNmt5w0g=
IV :VUdInSdIlCLS/D3AbXhhnQ==
json: {
"fingerprint" : "a7pNFC3Bnac7Y/k7/b+b4jHdH5CE/nbu23Mmj9pAhZw=",
"key" : "YJAG4xYTTQ0Ke3FBIDgmobERgbi/Tl/LYt9cNmt5w0g=",
"iv" : "VUdInSdIlCLS/D3AbXhhnQ=="
}

Am I overlooking something maybe?
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs-/***@public.gmane.org
To unsubscribe from this group, send email to
nodejs+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Kelsey Dawes
2013-06-27 14:13:37 UTC
Permalink
Hi,
Post by Thijs Koerselman
Post by Thijs Koerselman
var decipher = crypto.createDecipher('aes-256-cbc',binkey, biniv);
I think you want crypto.createDecipheriv here, instead of
crypto.createDecipher.

crypto.createDecipheriv(algorithm, key, iv)
creates and returns a decipher object, with the given algorithm, key and
iv. This is the mirror of
thecreateCipheriv()<http://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv>
above

-Kelsey
Post by Thijs Koerselman
Hi,
I'm trying to encrypt some string in C#, send it to a Node server and
TypeError: error:06065064:digital envelope
routines:EVP_DecryptFinal_ex:bad decrypt
at Decipher.Cipher.final (crypto.js:257:27)
I have tried different AES modes and key sizes but it didn't help. Just
for testing I'm sending a json post to the server containing base64 encoded
versions of key, iv and the encrypted string (fingerprint in this example).
var crypto = require('crypto');
var key = req.body.key;
var iv = req.body.iv;
var binkey = new Buffer(key, 'base64');
var biniv = new Buffer(iv, 'base64');
var crypted = req.body.fingerprint;
var bincrypted = new Buffer(crypted, 'base64');
var decipher = crypto.createDecipher('aes-256-cbc',binkey, biniv);
var dec = decipher.update(bincrypted,'binary','utf8');
dec += decipher.final('utf8');
console.log("dec", dec);
To be sure I'm using the same Aes mode and padding on the encryption this
is what's reported by my C# Aes, including the JSON being sent.
Keysize 256
Mode CBC
Padding PKCS7
Key: YJAG4xYTTQ0Ke3FBIDgmobERgbi/Tl/LYt9cNmt5w0g=
IV :VUdInSdIlCLS/D3AbXhhnQ==
json: {
"fingerprint" : "a7pNFC3Bnac7Y/k7/b+b4jHdH5CE/nbu23Mmj9pAhZw=",
"key" : "YJAG4xYTTQ0Ke3FBIDgmobERgbi/Tl/LYt9cNmt5w0g=",
"iv" : "VUdInSdIlCLS/D3AbXhhnQ=="
}
Am I overlooking something maybe?
--
--
Job Board: http://jobs.nodejs.org/
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs-/***@public.gmane.org
To unsubscribe from this group, send email to
nodejs+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Thijs Koerselman
2013-06-27 14:24:34 UTC
Permalink
Post by Kelsey Dawes
I think you want crypto.createDecipheriv here, instead of
crypto.createDecipher.
Ouch! I must have read over that about 30 times. I was assuming the two
flavors were based on the number of arguments you supplied to the same
create function. Thanks for pointing it out. It's working now!

I would prefer to use a password to derive the key from that, and append IV
before the data. That way I can just have one shared password between my
apps. How do I know in what way Node crypto derives that key from the
password so that I can match it in C#? Or the other way around?
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs-/***@public.gmane.org
To unsubscribe from this group, send email to
nodejs+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...