我正在尝试在 Mono C# 中加密一些数据,将其发送到 NodeJS 服务器并在那里解密。我试图弄清楚使用什么算法来匹配这两者。
我发送了用 base64 编码的加密字符串。所以我在 Javascript 中做了类似的事情,我知道用于加密我的 C# 应用程序中的数据的 key :
var decipher = crypto.createDecipher('aes192',binkey, biniv);
var dec = decipher.update(crypted,'base64','utf8');
dec += decipher.final('utf8');
console.log("dec", dec);
在 Mono 中,我使用以下方法创建我的 Cypher:
using System.Security.Cryptography;
using (Aes aesAlg = Aes.Create("aes192"))
我需要将正确的字符串传递给 Aes.Create() 以使其使用相同的算法,但我找不到它应该是什么。 “aes192”似乎不正确。
我不需要 aes192 这只是一个尝试。如果有意义,建议不同的加密风格。安全不是什么大问题。
以下是 .NET 和 Nodejs 文档的链接: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx http://nodejs.org/api/crypto.html
最佳答案
此代码适用于我的 Node.js 端,但请替换静态 iv,否则 aes 加密将无用。
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 位又名 32 个字符的 key ,此算法才能正常工作。
可能的 .NET 解决方案:这可能对您有所帮助 Example
关于c# - AES 在 .NET 中加密并使用 Node.js 加密解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306552/