Skip to content

Instantly share code, notes, and snippets.

@troufster
Created April 27, 2012 18:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save troufster/2511828 to your computer and use it in GitHub Desktop.
Save troufster/2511828 to your computer and use it in GitHub Desktop.
Decrypt .NET forms auth cookie in node.js
function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
//Raw cookie
var cookie = "B417B464CA63FE780584563D2DA4709B03F6195189044C26A29770F3203881DD90B1428139088D945CF6807CA408F201DABBADD59CE1D740F853A894692273F1CA83EC3F26493744E3D25D720374E03393F71E21BE2D96B6110CB7AC12E44447FFBD810D3D57FBACA8DF5249EB503C3DFD255692409F084650EFED205388DD8C08BF7B941E1AC1B3B70B9A8E09118D756BEAFF25834E72357FD40E80E76458091224FAE8";
//decryptionKey from issuers <machineKey>
var deckey = "FFA87B82D4A1BEAA15C06F6434A7EB2251976A838784E134900E6629B9F954B7";
var crypto = require('crypto');
var ivc = cookie, iv, cipherText, ivSize = 16, res = "";
ivc = new Buffer(ivc, 'hex');
iv = new Buffer(ivSize);
cipherText = new Buffer(ivc.length - ivSize);
ivc.copy(iv, 0, 0, ivSize);
ivc.copy(cipherText, 0, ivSize);
c = crypto.createDecipheriv('aes-256-cbc', hex2a(deckey), iv.toString('binary'));
res = c.update(cipherText, "binary", "utf8");
res += c.final('utf8');
console.log(res);
@qjleely
Copy link

qjleely commented Feb 24, 2015

are you sure your gist works? I got error on the last line of your code c.final('utf8')?

TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Decipheriv.Cipher.final (crypto.js:323:27)
at Object.decrypt (c:\Projects\xxx\decrypt-cookie-helper.js:9:1657)
at Context. (c:\Projects\Findly.Node.Restify.Pollinator\test\unit\decrpt-cookie-helper.spec.js:39:34)
at callFn (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runnable.js:251:21)
at Test.Runnable.run (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runnable.js:244:7)
at Runner.runTest (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:374:10)
at c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:452:12
at next (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:299:14)
at c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:309:7
at next (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:248:23)
at Object._onImmediate (c:\Projects\Findly.Node.Restify.Pollinator\node_modules\mocha\lib\runner.js:276:5)
at processImmediate as _immediateCallback

@qjleely
Copy link

qjleely commented Feb 24, 2015

I am using aes-192-cbc, any code need to be changed on your gist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment