Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PKCS5使用node.js crypto填充_Node.js_Cryptography_Node Crypto - Fatal编程技术网

PKCS5使用node.js crypto填充

PKCS5使用node.js crypto填充,node.js,cryptography,node-crypto,Node.js,Cryptography,Node Crypto,node.js crypto的文档表明填充会自动插入到输入流中。我有一个简单的测试程序,需要计算明文的SHA1散列,然后使用AES-256/CBC和PKCS5填充对20字节的散列进行加密。以下是我的测试代码: var fs = require('fs'); var crypto = require('crypto'); var key = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a

node.js crypto的文档表明填充会自动插入到输入流中。我有一个简单的测试程序,需要计算明文的SHA1散列,然后使用AES-256/CBC和PKCS5填充对20字节的散列进行加密。以下是我的测试代码:

var fs = require('fs');
var crypto = require('crypto');

var key = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f ];

var iv = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ];

var test_string = "This is the string I am signing";

// Create SHA1 hash of our string
var sha1Hash = crypto.createHash("sha1");
sha1Hash.update(test_string, "utf8");
var sha1 = sha1Hash.digest();

var aesCipher =
        crypto.createCipheriv("aes-256-cbc", (new Buffer(key)), (new Buffer(iv)));
aesCipher.update(sha1, "binary");
var encrypted = aesCipher.final("binary");

fs.writeFile('data/node_encrypted.bin', encrypted, function (error) {
    if (error)
        throw error;
    console.log("File saved!");
});

但是,这将生成一个长度仅为23字节的文件。我希望通过适当的PKCS5填充,输出将是32字节。我遗漏了什么?

首先,您忽略了
aesciper.update()的返回值,从而丢弃了加密内容的第一部分。另外,由于
update()
没有指定输出编码,因此它返回一个缓冲区,而
final()
则返回一个“二进制”字符串。您可以尝试:

var encrypted = Buffer.concat([
  aesCipher.update(sha1, "binary"),
  aesCipher.final()
]);

首先,您忽略了
aesciper.update()的返回值,从而丢弃了加密内容的第一部分。另外,由于
update()
没有指定输出编码,因此它返回一个缓冲区,而
final()
则返回一个“二进制”字符串。您可以尝试:

var encrypted = Buffer.concat([
  aesCipher.update(sha1, "binary"),
  aesCipher.final()
]);

非常感谢。这就是问题所在。我不清楚cipher.final()不会生成全部加密内容。谢谢!这帮助我解决了类似的解密问题,我看到了部分解密的字符串。用类似的方法解决了这个问题:
return Buffer.concat([decipher.update(new Buffer(data,'base64')),decipher.final()))。toString('utf8')
I同意,@GregR-节点文档在这里似乎不是很清楚:“在调用cipher.final()之前,可以使用新数据多次调用cipher.update()方法。”我的理解是final()将累积所有update()调用。谢谢。这就是问题所在。我不清楚cipher.final()不会生成全部加密内容。谢谢!这帮助我解决了类似的解密问题,我看到了部分解密的字符串。用类似的方法解决了这个问题:
return Buffer.concat([decipher.update(new Buffer(data,'base64')),decipher.final()))。toString('utf8')
I同意,@GregR-节点文档在这里似乎不是很清楚:“在调用cipher.final()之前,可以使用新数据多次调用cipher.update()方法。”我的理解是final()将累积所有update()调用。我也有同样的问题。很好的问题和答案。有同样的问题。很好的问题和答案。