问题描述
我在.Net中创建了一个加密的cookie,我试图解密它在nodejs中的内容。但是nodejs继续抛出异常TypeError:DecipherFinal fail
I've created an encrypted cookie in .Net and I'm trying to decrypt it's content in nodejs. But nodejs keeps throwing the exception "TypeError: DecipherFinal fail"
在.NET中我使用AES加密方法与键
In .Net I'm using the AES encryption method with the key
932D86BB1448EEAA423F38495A2290746D81C27E55D1DC264279537006D6F4CC。
我的web.config文件有以下行
My web.config file has the following row
<machineKey validationKey="A5326FFC9D3B74527AECE124D0B7BE5D85D58AFB12AAB3D76319B27EE57608A5A7BCAB5E34C7F1305ECE5AC78DB1FFEC0A9435C316884AB4C83D2008B533CFD9"
decryptionKey="932D86BB1448EEAA423F38495A2290746D81C27E55D1DC264279537006D6F4CC"
validation="SHA1" decryption="AES" />
在.Net中生成我的cookie的代码如下:
And the code that generates my cookie in .Net looks like this:
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));
解密cookie的nodejs代码是
The nodejs code that decrypts the cookie is
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;
}
我很失望, 。
推荐答案
您可以在这里看到Encryp和Decrypt的源代码,所有的不同的可能性(Framework20SP1,Framework20SP2, etc)
You can see the source code of Encryp and Decrypt here with all the different possibilities (Framework20SP1, Framework20SP2, etc)
我读了这段代码的时间,但一旦你得到它,就可以为你的特定加密设置写一个简单的代码。
It took me hours to read that code, but once you got it, it's possible to write a simple code just for your specific encryption settings.
这篇关于解密node.js中的.Net cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!