简体   繁体   English

AES在.NET中加密并使用Node.js加密解密?

[英]AES encrypt in .NET and decrypt with Node.js crypto?

I'm trying to encrypt some data in Mono C#, send it to a NodeJS server and decrypt it there. 我正在尝试加密Mono C#中的一些数据,将其发送到NodeJS服务器并在那里解密。 I'm trying to figure out what algorithms to use to match the two. 我正在试图弄清楚用什么算法来匹配这两者。

I send the encrypted string encoded with base64. 我发送用base64编码的加密字符串。 So I do something like this in Javascript, where I know the key which was used to encrypt the data in my C# application: 所以我在Javascript中做了类似的事情,在那里我知道用于加密C#应用程序中数据的密钥:

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

In Mono I create my Cypher with: 在单声道中,我创建了我的Cypher:

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

I need to pass the correct string to Aes.Create() in order to have it use the same algorithm, but I can't find what it should be. 我需要将正确的字符串传递给Aes.Create()以使其使用相同的算法,但我找不到它应该是什么。 "aes192" is not correct it seems. 似乎“aes192”不正确。

I don't need aes192 this was just a tryout. 我不需要aes192这只是一个试用版。 Suggest a different encryption flavor if it makes sense. 如果有意义,建议不同的加密风格。 Security is not much of an issue. 安全性不是问题。

Here are links to .NET and Nodejs docs: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx http://nodejs.org/api/crypto.html 以下是.NET和Nodejs文档的链接: http//msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx http://nodejs.org/api/crypto.html

This code works for my Node.js side, but please replace the static iv, otherwhise aes encryption would be useless. 这段代码适用于我的Node.js端,但是请替换静态iv,否则加密将是无用的。

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;
}

I hope this helps You. 我希望这可以帮助你。

NOTE: You need to deliver an 265bit aka 32 character key for this algorithm to work. 注意:您需要提供一个265位的32位字符密钥才能使此算法正常工作。

POSSIBLE .NET SOLUTION: This may help you Example 可能的.NET解决方案:这可能会帮助你实例

You should simply write new AesManaged() . 你应该简单地编写new AesManaged()
You don't need to call Create() . 您不需要调用Create()

You then need to set Key and IV , then call CreateDecryptor() and put it in a CryptoStream . 然后,您需要设置KeyIV ,然后调用CreateDecryptor()并将其放入CryptoStream

It turned out to be a stupid mistake. 结果证明这是一个愚蠢的错误。 I thought the create function in Node.js could take a variable argument count. 我认为Node.js中的create函数可以采用变量参数计数。 Turns out you need to call the createDecipheriv() instead. 事实证明你需要调用createDecipheriv()。

Just for the record, you can easily check the padding and mode by looking at those properties in the Aes object. 只是为了记录,您可以通过查看Aes对象中的这些属性来轻松检查填充和模式。 The defaults are CBC and PKCS7. 默认值为CBC和PKCS7。 That padding is also used in nodejs crypto. 该填充也用于nodejs加密。 So a for a 128 key size my code to decrypt a base64 encoded string would be: 因此,对于128密钥大小,我的代码解密base64编码的字符串将是:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM