Martin Clemens Bloch
Martin Clemens Bloch

Reputation: 1097

Google CryptoJS AES result too long by 1 block

Hello I'm using the following passphrase: "test". This generates the AES key: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" IV is 0.

As a test I tried to encrypt this key. Since I will always use this program with 256 bits I cut the encrypted cipher after 64 hex characters (256 bits).

However when I decrypt this cut ciphertext I'm missing the last 16 characters (128bits) of my key.

This is the size of one AES block, but my key is 256 bits so it seems weird to me.

My question is: Why is my AES result too long? And: Can I do this correctly so its 64 chars only?

(CryptoJS is the google crypto library for javascript.)

function go(){
    var shakey = CryptoJS.SHA256(document.getElementById("t3").value); //Textbox key here.
    var hash =CryptoJS.enc.Hex.parse(CryptoJS.enc.Hex.stringify(shakey));
    var key = CryptoJS.enc.Hex.stringify(hash);
    var iv  = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');

    var encHex = CryptoJS.enc.Hex.parse(document.getElementById("t1").value);

    var encrypted = CryptoJS.AES.encrypt(encHex, hash, { iv: iv }); //Textbox input here.

    var encObj = {ciphertext:CryptoJS.enc.Hex.parse(document.getElementById("t2").value)}; //Textbox decrypt here

    var decrypted = CryptoJS.AES.decrypt(encObj, hash, { iv: iv});

    var encResult = (encrypted.ciphertext+"").length > 63 ? (encrypted.ciphertext+"").substring(0,64) : (encrypted.ciphertext+"");

    document.getElementById("p1").innerHTML=encResult;
    document.getElementById("p2").innerHTML=decrypted;
}

Upvotes: 2

Views: 949

Answers (1)

Reid
Reid

Reputation: 19419

Disable padding using the NoPadding option. The default is PKCS#7 padding, which will always apply padding to the plaintext before encryption. In the case of a full block, it will pad out another full block, which makes the result one block longer than you would expect.

Upvotes: 2

Related Questions