4

I am trying to figure out the Node.js Crypto library and how to use it properly for my situation.

My Goal is:

key in hex string 3132333435363738313233343536373831323334353637383132333435363738

text in hex string 46303030303030303030303030303030

ciphered text in hex string 70ab7387a6a94098510bf0a6d972aabe

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

This is what I have to far, it's not working the way I would expect it to work. My best guess is that the input and output types are incorrect for the cipher function. The only one that works is utf8 if I use hex it fails with a v8 error. Any ideas on what I should convert or change to get it to 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 output in crypted in this example is 8cfdcda0a4ea07795945541e4d8c7e35 which is not what I would expect.

gevorg
  • 4,835
  • 4
  • 35
  • 52
Adam Magaluk
  • 1,716
  • 20
  • 29
  • Your goal implies the absence of an IV. Do you really want that? I also strongly recommend adding a MAC. – CodesInChaos Aug 19 '12 at 15:03
  • I suspect the crypto library automatically adds a random IV and padding, resulting in a 3 block (48 byte) output. Which is more appropriate in most situations than paddingless ECB. – CodesInChaos Aug 19 '12 at 15:05
  • I plan to use a IV, I left it out to simplify the question. Im not sure what you mean about a MAC? – Adam Magaluk Aug 19 '12 at 15:05
  • An integrity check that ensures nobody tampered with your ciphertext. Without it you open yourself up to a number of attacks, such as padding oracles. – CodesInChaos Aug 19 '12 at 15:08
  • Looking into the NodeJS documentation I only see a two argument createCipher factory method. What's the version you are using & is has the code above been tested? – Maarten Bodewes Aug 19 '12 at 23:27

1 Answers1

1

Your code is using aes-256-cbc when the website you are deriving test vectors from is using ecb mode. Also, you are calling createCipher, but with ECB you should use createCipheriv with no IV (see nodeJS: can't get crypto module to give me the right AES cipher outcome),

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 output of running that code is not exactly what I expect, but the first block of the encrypted output matches your expectation. Probably another parameter that needs to be tweaked.:

$ node test-aes-ecb.js 
> 70ab7387a6a94098510bf0a6d972aabeeebbdaed7324ec4bc70d1c0343337233
? 70ab7387a6a94098510bf0a6d972aabe
Community
  • 1
  • 1
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155