2012-09-04 5 views
1

에서 IV와 키를 얻기 위해 어떻게/암호화 작동하고 Node.js를 데이터, 암호 해독 할 수있는 다음과 같은 코드가 있습니다암호 해독 AES256은 Node.js를에서 .NET 데이터를 암호화 - 암호

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을 두 개의 별도 시스템간에 데이터를 공유 할 수 있도록 C#/.NET에서 동일한 작업을 수행 할 수 있기를 원합니다. 그러나 .NET에서 본 코드는 entrypt/decrypt하기 위해 Key와 IV를 필요로합니다. 이 파일들은 node.js 암호화 라이브러리의 암호문에서 어떻게 파생 되었습니까? node.js source 가입일

답변

2

는 I이 발견 : 키와 IV가 다음에 사용될 수

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

: I는 다음과 같이 사용될 수있다 this question에 EVP_BytesToKey의 교류 # 구현을 발견

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

암호 해독 할 RijndaelManaged의 인스턴스 encrytedBytes

+0

암호문을 유니 코드와 함께 사용하면 바이트 인코딩이 node.js와 다릅니다. 뭐 ... 힌트 요? – Tracker1

+0

Nodejs는 [Latin1 인코딩] (https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password)을 사용합니다. ASCII 대신'Encoding.GetEncoding ("ISO-8859-1")'을 사용해야합니다. –