Node.jsencryptioninput/输出types

我想弄清楚Node.jsencryption库,以及如何正确使用它适合我的情况。

我的目标是:

键入hexstring3132333435363738313233343536373831323334353637383132333435363738

在hexstring中的文本46303030303030303030303030303030

在hexstring中的encryption文本70ab7387a6a94098510bf0a6d972aabe

我正在通过ac 256的实现来testing这个,并通过一个网站http://www.hanewin.net/encrypt/aes/aes-test.htm

这是我迄今为止所做的,并不像我期望的那样工作。 我最好的猜测是input和输出types对于密码函数是不正确的。 唯一的作品是utf8,如果我使用hex,它失败了一个V8错误。 任何想法,我应该转换或改变,以使其工作。

var keytext = "3132333435363738313233343536373831323334353637383132333435363738"; var key = new Buffer(keytext, 'hex'); var crypto = require("crypto") var cipher = crypto.createCipher('aes-256-cbc',key,'hex'); var decipher = crypto.createDecipher('aes-256-cbc',key,'hex'); var text = "46303030303030303030303030303030"; var buff = new Buffer(text, 'hex'); console.log(buff) var crypted = cipher.update(buff,'hex','hex') 

在这个例子中encryption输出是8cfdcda0a4ea07795945541e4d8c7e35这不是我所期望的。

你的代码是使用aes-256-cbc时候,你从中获取testing向量的网站正在使用ecb模式。 另外,您正在调用createCipher ,但使用ECB时,您应该使用createCipheriv带IV的createCipheriv (请参阅nodeJS:无法使encryption模块为我提供正确的AESencryption结果 ),

这里有一些代码演示了这一点:

 var crypto = require("crypto"); var testVector = { plaintext : "46303030303030303030303030303030", iv : "", key : "3132333435363738313233343536373831323334353637383132333435363738", ciphertext : "70ab7387a6a94098510bf0a6d972aabe"}; var key = new Buffer(testVector.key, "hex"); var text = new Buffer(testVector.plaintext, "hex"); var cipher = crypto.createCipheriv("aes-256-ecb", key, testVector.iv); var crypted = cipher.update(text,'hex','hex'); crypted += cipher.final("hex"); console.log("> " + crypted); console.log("? " + testVector.ciphertext); 

运行该代码的输出不是我所期望的,但encryption输出的第一个块与您的期望相符。 可能是需要调整的另一个参数:

 $ node test-aes-ecb.js > 70ab7387a6a94098510bf0a6d972aabeeebbdaed7324ec4bc70d1c0343337233 ? 70ab7387a6a94098510bf0a6d972aabe