AES在.NET中encryption并使用Node.jsencryption解密?

我试图encryptionMono C#中的一些数据,将其发送到NodeJS服务器并在那里解密。 我想弄清楚使用什么algorithm来匹配这两个。

我发送用base64编码的encryptionstring。 所以我在JavaScript中做了这样的事情,我知道用于encryption我的C#应用​​程序中的数据的密钥:

var decipher = crypto.createDecipher('aes192',binkey, biniv); var dec = decipher.update(crypted,'base64','utf8'); dec += decipher.final('utf8'); console.log("dec", dec); 

在单声道我创build我的Cypher:

 using System.Security.Cryptography; using (Aes aesAlg = Aes.Create("aes192")) 

我需要将正确的string传递给Aes.Create(),以使它使用相同的algorithm,但是我找不到它应该是什么。 看起来“aes192”是不正确的。

我不需要aes192这只是一个试用。 build议一个不同的encryption风格,如果它是有道理的。 安全不是一个问题。

这里是链接到.NET和Nodejs文档: http : //msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx http://nodejs.org/api/crypto.html

这个代码适用于我的Node.js端,但请replace静态的iv,其他的aesencryption将是无用的。

 var crypto = require('crypto'); function encrypt(data, key) { key = key || new Buffer(Core.config.crypto.cryptokey, 'binary'), cipher = crypto.createCipheriv('aes-256-cbc', key.toString('binary'), str_repeat('\0', 16)); cipher.update(data.toString(), 'utf8', 'base64'); return cipher.final('base64'); } function decipher(data, key) { key = key || new Buffer(Core.config.crypto.cryptokey, 'binary'), decipher = crypto.createDecipheriv('aes-256-cbc', key.toString('binary'), str_repeat('\0', 16)); decipher.update(data, 'base64', 'utf8'); return decipher.final('utf8'); } function str_repeat(input, multiplier) { var y = ''; while (true) { if (multiplier & 1) { y += input; } multiplier >>= 1; if (multiplier) { input += input; } else { break; } } return y; } 

我希望这可以帮助你。

注意:您需要提供一个265位aka 32个字符的密钥为此algorithm工作。

可能的.NET解决scheme:这可能会帮助你的例子

你应该简单地写new AesManaged()
你不需要调用Create()

然后你需要设置KeyIV ,然后调用CreateDecryptor()并把它放在一个CryptoStream

结果是一个愚蠢的错误。 我认为Node.js中的创build函数可能需要一个可变的参数。 原来你需要调用createDecipheriv()来代替。

为了logging,您可以通过查看Aes对象中的这些属性来轻松地检查填充和模式。 默认值是CBC和PKCS7。 该填充也用于nodejsencryption。 所以一个128密钥大小我的代码来解密一个base64编码的string将是:

 var crypto = require('crypto'); var binkey = new Buffer(key, 'base64'); var biniv = new Buffer(iv, 'base64'); var decipher = crypto.createDecipheriv('aes-128-cbc', binkey, biniv); var decrypted = decipher.update(crypted,'base64','utf8'); decrypted += decipher.final('utf8'); console.log("decrypted", decrypted);