Descifrar .Net cookie en nodejs
He creado una cookie cifrada en .Net y estoy intentando descifrar su contenido en nodejs. Pero nodejs sigue lanzando la excepción "TypeError: DecipherFinal fail"
En .Net estoy usando el método de cifrado AES con la clave
932D86BB1448EEAA423F38495A2290746D81C27E55D1DC264279537006D6F4CC.
Mi archivo web.config tiene la siguiente fila
<machineKey validationKey="A5326FFC9D3B74527AECE124D0B7BE5D85D58AFB12AAB3D76319B27EE57608A5A7BCAB5E34C7F1305ECE5AC78DB1FFEC0A9435C316884AB4C83D2008B533CFD9"
decryptionKey="932D86BB1448EEAA423F38495A2290746D81C27E55D1DC264279537006D6F4CC"
validation="SHA1" decryption="AES" />
Y el código que genera mi cookie en .Net se ve así:
var ticket = new FormsAuthenticationTicket(0, "test", DateTime.Now, DateTime.Now.AddYears(1), true, "test");
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(cookieName, encryptedTicket));
El código de nodejs que descifra la cookie es
var crypto = require('crypto');
var logger = require('winston');
var deckey = "932D86BB1448EEAA423F38495A2290746D81C27E55D1DC264279537006D6F4CC";
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;
}
function decrypt(cookie) {
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);
iv = new Buffer(Array(16));
c = crypto.createDecipheriv('aes-256-cbc', hex2a(deckey), iv.toString());
res = c.update(cipherText, 'binary');
res += c.final('binary'); //<-- throws TypeError: DecipherFinal fail
return res;
}
Estoy algo perdido y agradecería consejos o ideas sobre cuál podría ser el problema.