简体   繁体   English

aes-256-cbc加密和解密

[英]aes-256-cbc encryption and decryption

I am encrypting an audio file using crypto npm module in node.js and trying to decrypt it in the android side using the same algorithm and the key. 我正在使用node.js中的crypto npm模块加密 音频文件,并尝试使用相同的算法和密钥在android端对其进行解密

The encryption code is : 加密代码为:

encyption parameters 输入参数

var crypto = require('crypto'),
    algorithm = 'aes-256-cbc',
    password = 'somepassword';  //encyption parameters

encryption function 加密功能

function encrypt(buffer) {
    var cipher = crypto.createCipher(algorithm, password);
    var crypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
    return crypted;
}

Now,for Decryption , we need to use some IV( Initialisation Vector ) and as research says not to use same IV for any two files . 现在,对于解密 ,我们需要使用一些IV( 初始化向量 ),并且研究表明不要对任何两个文件使用相同的IV

So, I just want to know now how customized IV can be set and random or separate IVs can be generated for each file in node.js using crypto or any other module during encryption. 因此,我现在只想知道如何设置自定义IV,以及如何在加密过程中使用crypto或任何其他模块 node.js中的每个文件生成随机或单独的IV

It will be great if someone can help me out in this. 如果有人可以在这方面帮助我,那将是很棒的。

to create the IV, use the following command to get 16 random bytes: 要创建IV,请使用以下命令获取16个随机字节:

var iv = crypto.randomBytes(16)

then, when creating the cipher, change 然后,在创建密码时,更改

var cipher = crypto.createCipher(algorithm, password);

to

var cipher = crypto.createCipheriv(algorithm, password, iv);

The IV will not be attached to the resulting ciphertext, so you will need to send it separately to the android side (it can be sent in plaintext). IV不会附加到生成的密文上,因此您需要将其单独发送到android端(可以以纯文本形式发送)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM