ifeoluwa king
ifeoluwa king

Reputation: 531

Crypto JS: TripleDES not encrypting properly

When using crypto js tripleDES to encrypt, I noticed that all strings to encrypt which are of small length e.g "01", "111", "20" encrypt properly but with long strings like "5000021234567890000", the output isn't correct or is not the value I am expecting. For example for this "5000021234567890000", I am expecting this: "HctDaKc/U9avOwZMSS5mEi62kfojDHA4" but I get this instead: HctDaKc/U9bNnFy6eZHqHj3SemorJxQM.

This is the code I found and using to encrypt

let key = CryptoJS.MD5(enckey);
key.words[4] = key.words[0];
key.words[5] = key.words[1];
let iv = CryptoJS.lib.WordArray.create(64/8);
CryptoJS.TripleDES.encrypt("5000021234567890000", key, {iv: iv});

Your help would be very much appreciated.

Upvotes: 1

Views: 1204

Answers (1)

zaph
zaph

Reputation: 112855

Looking at the encrypted data the first blocks match and the following two blocks do not.

    HctDaKc/U9avOwZMSS5mEi62kfojDHA4
    1DCB4368A73F53D6 AF3B064C492E6612 2EB691FA230C7038

    HctDaKc/U9bNnFy6eZHqHj3SemorJxQM
    1DCB4368A73F53D6 CD9C5CBA7991EA1E 3DD27A6A2B27140C

The IV is presumably all 0x00 bytes.

THis indicates one is using ECB m ode and the other is using CBC mode.

See Block cipher mode of operation, specifically ECB and CBC modes.

ECB mode encrypted each block independly, CBC mode xors the previous block with the data to be encrypted and for the first block the IV. Since the IV is all 0x00 values no change is made to the first block.

Options can be specified in the createion of the encryptor, you need to see the documentation page, good luck with that.

It would look something like:

encryptor = crypto.createCipheriv( mode, key, iv)

where mode is one of: 'des-ede', 'des-ede-cbc', 'des-ede3', 'des-ede3-cbc', 'des3'

Upvotes: 2

Related Questions