Node.js Crypto I / O Types

I am trying to calculate the Node.js Crypto library and how to use it correctly for my situation.

My goal:

the key in the hexadecimal string 3132333435363738313233343536373831323334353637383132333435363738

hexadecimal text 4630303030303030303030303030303030

encrypted text in hexadecimal string 70ab7387a6a94098510bf0a6d972aabe

I am testing this through the c implementation of AES 256 and through the website http://www.hanewin.net/encrypt/aes/aes-test.htm

This is what I need, far from the way I expect it to work. My best guess is that the input and output types are incorrect for the encryption function. The only one that works is utf8, if I use hex it fails with v8 error. Any ideas on what I should convert or modify to make it work.

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') 

The encrypted output in this example is 8cfdcda0a4ea07795945541e4d8c7e35, which I did not expect.

+4
source share
1 answer

Your code uses aes-256-cbc when the website from which you will output the test vectors uses ecb mode. Also, you call createCipher , but with ECB you have to use createCipheriv without IV (see nodeJS: it is not possible to get a crypto module to give me the correct AES encryption result )

Here is some code that demonstrates this:

 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); 

The result of executing this code is not quite what I expect, but the first block of encrypted output matches your expectation. Perhaps another parameter that needs to be configured .:

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

All Articles