从Node.js的解密AES256加密的数据在.NET中 - 如何获得密码IV和密钥 [英] Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase
问题描述
我有以下的code加密/解密Node.js的数据,这是工作的:
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');
我希望能够做同样的在C#/。NET,这样我可以分享两个独立的系统之间的数据。然而,code,我已经看到在.NET需要一个密钥和四entrypt /解密。如何在Node.js的加密库,这些源自密码?
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?
推荐答案
从的 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\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);
我发现C#实现EVP_BytesToKey的<一个href="http://stackoverflow.com/questions/8008253/c-sharp-version-of-openssl-evp-bytestokey-method">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==");
键和IV然后可以在 RijndaelManaged的
的实例用来解密 encrytedBytes
这篇关于从Node.js的解密AES256加密的数据在.NET中 - 如何获得密码IV和密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!