Search code examples
javascriptencryptioncryptojs

Encrypting and decrypting with DES and Base64 with JavaScript


I am encrypting something previously encoded in base64.

The steps are:
1. Encode the plain (readable) text in base64
2. Encrypt the base64 encoded text with DES/CBC/PKCS7PADDING using CryptoJS
3. Encode again in base64 the information obtained in step 2

This is my code in Javascript:

function encryptDesCbcPkcs7Padding(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var iv = new Uint8Array(0);
    var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));

    var encrypted = CryptoJS.DES.encrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });


    return encrypted;//.toString(CryptoJS.enc.Utf8);
}

var plainText = "hola";
console.log("Plain text: ", plainText);
var base64Coded = window.btoa(plainText);
console.log("Base64 coded text: ", base64Coded);

var encrypted = encryptDesCbcPkcs7Padding(base64Coded, "12345678");
console.log("Encrypted: ", encrypted);
var finalEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
console.log("Final encrypted: ", finalEncrypted);
alert("Final encrypted: " + finalEncrypted);

With this code I get the information encryped with the previous steps.

Problem: I can't make the reverse steps. What am I doing wrong?

These is the code I use for the reverse:

function decryptDesCbcPkcs7Padding(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var iv = new Uint8Array(0);
    var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));

    var decrypted = CryptoJS.DES.decrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });


    return decrypted.toString(CryptoJS.enc.Utf8);
}

var base64Decoded = CryptoJS.enc.Base64.parse(finalEncrypted).toString();
console.log("Base64 decoded", base64Decoded);

var decrypted = decryptDesCbcPkcs7Padding(base64Decoded, "12345678");
console.log("Decrypted: ", decrypted);

var finalDecrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
console.log("Final encrypted: ", finalEncrypted);
alert("Final encrypted: " + finalEncrypted);

I can't make this code work, what am I doing wrong?


Solution

  • There are many problems with the encodings in your code in addition to copy-paste errors. Here is the fixed code.

    function encryptDesCbcPkcs7Padding(message, key) {
        var keyWords = CryptoJS.enc.Utf8.parse(key);
        var ivWords = CryptoJS.lib.WordArray.create([0, 0]);
        var encrypted = CryptoJS.DES.encrypt(message, keyWords, { iv: ivWords});
      
        return encrypted;//.toString(CryptoJS.enc.Utf8);
    }
    
    var plainText = "hola";
    console.log("Plain text: ", plainText);
    var base64Coded = window.btoa(plainText);
    console.log("Base64 coded text: ", base64Coded);
    
    var encrypted = encryptDesCbcPkcs7Padding(base64Coded, "12345678");
    console.log("Encrypted: ", encrypted);
    var finalEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
    console.log("Final encrypted: ", finalEncrypted);
    alert("Final encrypted: " + finalEncrypted);
    
    function decryptDesCbcPkcs7Padding(message, key) {
        var keyWords = CryptoJS.enc.Utf8.parse(key);
        var ivWords = CryptoJS.lib.WordArray.create([0, 0]);
    
        var decrypted = CryptoJS.DES.decrypt({ciphertext: message}, keyWords, { iv: ivWords });
    
        return decrypted.toString(CryptoJS.enc.Utf8);
    }
    
    var base64Decoded = CryptoJS.enc.Base64.parse(finalEncrypted);
    console.log("Base64 decoded", base64Decoded);
    
    var decrypted = decryptDesCbcPkcs7Padding(base64Decoded, "12345678");
    console.log("Decrypted: ", decrypted);
    
    var finalDecrypted = CryptoJS.enc.Base64.parse(decrypted.toString(CryptoJS.enc.Utf8)).toString(CryptoJS.enc.Utf8);
    console.log("Final decrypted: ", finalDecrypted);
    alert("Final decrypted: " + finalDecrypted);
    <script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/tripledes.js"></script>