Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase

前端 未结 1 357
难免孤独
难免孤独 2021-01-25 05:19

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

var cipher = crypto.createCipher(\'aes256\', \'passphrase\');  
var encrypted = c         


        
相关标签:
1条回答
  • 2021-01-25 05:45

    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

    0 讨论(0)
提交回复
热议问题