AES encrypt in Node.js Decrypt in PHP. Fail.

后端 未结 7 1935
陌清茗
陌清茗 2020-12-09 19:50

In node.js, I use the build in function to encrypt data like that:

var text = \"Yes\";
var password = \"123456\";
var encrypt = crypto.createCipher(\'aes-256         


        
7条回答
  •  庸人自扰
    2020-12-09 20:31

    If you're stuck with a third-party library which uses MCRYPT_RIJNDAEL_256, know that the 256 specifies the block size, not the key size. AES uses a fixed block-size of 128 bits, and openssl does not implement more generic Rijndael algorithms. To circumvent this I published a module that binds to libmcrypt, just as PHP does. It's a pretty limited use-case, but it ensures it will be compatible with 256-bit block size rijndael.

    If you're using this in PHP

    mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plaintext, MCRYPT_MODE_ECB);
    mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $ciphertext, MCRYPT_MODE_ECB);
    

    You can do the same in Node:

    var rijndael = require('node-rijndael');
    
    // straight through (must be buffers)
    rijndael.encrypt(plaintext, key);
    rijndael.decrypt(ciphertext, key);
    
    // or bound (can take a string for the key and an encoding)
    var rijn = rijndael(key);
    rijn.encrypt(plaintext); // gotta be a buffer again for implementation simplicity
    rijn.decrypt(ciphertext);
    

    node-rijndael on GitHub

    node-rijndael on npm

提交回复
热议问题