从node.js中解密AES256加密数据,如何从密码中获取IV和Key [英] Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase

查看:447
本文介绍了从node.js中解密AES256加密数据,如何从密码中获取IV和Key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var cipher = crypto .createCipher('aes256','passphrase'); 
var encrypted = cipher.update(test,'utf8','base64')+ cipher.final('base64');

var decipher = crypto.createDecipher('aes256','passphrase');
var plain = decipher.update(encrypted,'base64','utf8')+ decipher.final('utf8');

我希望能够在C#/ .NET中执行相同操作,以便我可以共享数据在两个单独的系统之间。但是我在.NET中看到的代码需要一个Key和IV来进入/解密。这些派生自node.js Crypto库中的密码短语?

解决方案

node.js源我发现这个:

  bool CipherInit(char * cipherType,char * key_buf,int key_buf_len){
cipher = EVP_get_cipherbyname(cipherType);
if(!cipher){
fprintf(stderr,node-crypto:Unknown cipher%s\\\
,cipherType);
返回false;
}

unsigned char key [EVP_MAX_KEY_LENGTH],iv [EVP_MAX_IV_LENGTH];
int key_len = EVP_BytesToKey(cipher,EVP_md5(),NULL,
(unsigned char *)key_buf,key_buf_len,1,key,iv);

我在这个问题可以这样使用:

  byte [] key,iv; 
DeriveKeyAndIV(Encoding.ASCII.GetBytes(passphrase),null,1,out key,out iv);

//这是node.js给我的base64加密数据
var cryptotedBytes = Convert.FromBase64String(b3rbg + mniw7p9aiPUyGthg ==);

然后可以在 RijndaelManaged的实例中使用键和IV code>解密 encryptedtedBytes


I have the following code to encrypt/decrypt data in node.js, which is working:

var cipher = crypto.createCipher('aes256', 'passphrase');  
var encrypted = cipher.update("test", 'utf8', 'base64') + cipher.final('base64');

var decipher = crypto.createDecipher('aes256', 'passphrase');   
var plain = decipher.update(encrypted, 'base64', 'utf8') + decipher.final('utf8');

I would like to be able to do the same in C#/.NET so that I can share data between two seperate systems. However the code that I have seen in .NET requires a Key and IV to entrypt/decrypt. How are these derived from the passphrase in the node.js Crypto library?

解决方案

From the node.js source I found this:

 bool CipherInit(char* cipherType, char* key_buf, int key_buf_len) {
cipher = EVP_get_cipherbyname(cipherType);
if(!cipher) {
  fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
  return false;
}

unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
int key_len = EVP_BytesToKey(cipher, EVP_md5(), NULL,
  (unsigned char*) key_buf, key_buf_len, 1, key, iv);

I found a c# implementation of EVP_BytesToKey in this question which can be used like this:

byte[] key, iv;
DeriveKeyAndIV(Encoding.ASCII.GetBytes("passphrase"),null, 1, out key, out iv);

                     //this is what node.js gave me as the base64 encrypted data
var encrytedBytes = Convert.FromBase64String("b3rbg+mniw7p9aiPUyGthg==");

The key and IV can then be used in an instance of RijndaelManaged to decrypt encrytedBytes

这篇关于从node.js中解密AES256加密数据,如何从密码中获取IV和Key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆