Monday, 5 June 2017

How do I decipher with a key using nodejs crypto?

I am working on Cryptopals' Challenge 1.7 and I have run into a blocker. I am attempting to use node.js' Crypto module but I am not sure if I am using it incorrectly.

I have saved Cryptopals' encrypted file as 7.txt. I know that the file is correct because I can decrypt it on my machine using the following command (Thanks to Rumata888's comment):

openssl enc -aes-128-ecb -in res/7.txt -a -d -K 59454c4c4f57205355424d4152494e45 -nosalt

After more digging I figured out that 59454c4c4f57205355424d4152494e45 is the key given in the challenge, "YELLOW SUBMARINE", in hex.

I have tried to supply that to the decipher in both hex and plaintext but neither seems to work. I keep getting the error:

error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

I have tried using stream piping as well, but I don't think the issue is in how I pipe together the data. I think I'm just misusing the decipher.

My process is:

  1. Load the input file
  2. Base64 decode the input data
  3. Create a decipher using AES 128 ECB with the key YELLOW SUBMARINE in either hex or plaintext
  4. Decipher the decoded input text
  5. Print the deciphered decoded input text

My code is as follows:

const crypto = require("crypto"),
    fs = require("fs"),
    assert = require("assert");

// Keep track of the key and the key in hex
const keyString = "YELLOW SUBMARINE",
    keyHexString = new Buffer(keyString).toString("hex");

// Sanity check that we got the right key
assert.strictEqual(keyHexString, "59454c4c4f57205355424d4152494e45");

// Read in the encrypted text
fs.readFile("res/7.txt", "utf8", (err, data) => {
    let decodedData = Buffer.from(data, "base64").toString(),
        // Try deciphering with the hex key
        decipher = crypto.createDecipher("aes-128-ecb", keyHexString),
        // Try deciphering with the plain text key
        //decipher = crypto.createDecipher("aes-128-ecb", keyString),
        deciphered = decipher.update(decodedData, "utf8", "utf8");
    // Finish the deciphering
    deciphered += decipher.final("utf8");
    console.log(deciphered);
});



via zero298

No comments:

Post a Comment