JSFiddle - React, Tailwind, and code Playground

by Vincent Wang

HTML

<SCRIPT SRC="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></SCRIPT>

<body onload="testTripleDESEncryption();">

Encryption key:<span id ="encryptionKeySpan"></span><br/>
Encryption iv:<span id ="encryptionIVSpan"></span><br/>    
Encrypted Value:<span id ="encryptSpan"></span><br/>
Decrypted Value:<span id ="decryptSpan"></span>
    
</body>

JavaScript

// Convert a byte array to a hex string
 function bytesToHex (bytes) {
     for (var hex = [], i = 0; i < bytes.length; i++) {
         hex.push((bytes[i] >>> 4).toString(16));
         hex.push((bytes[i] & 0xF).toString(16));
     }
     return hex.join("");
 }

function testTripleDESEncryption()
{  
    var keyByte = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
    var key = CryptoJS.enc.Hex.parse(bytesToHex(keyByte)); 
    //var key = CryptoJS.enc.Hex.parse('0102030405060708090a0b0c0d0e0f101112131415161718'); 
    
    //alert('Encryption key: ' + key);    
    $("#encryptionKeySpan").text(key);  
       
    var ivByte =  [ 65, 110, 68, 26, 69, 178, 200, 219 ];                                                  
    var iv  = CryptoJS.enc.Hex.parse(bytesToHex(ivByte));
    //var iv = CryptoJS.enc.Hex.parse('416e441a45b2c8db'); 

    $("#encryptionIVSpan").text(iv);  
    
    var encrypted = CryptoJS.TripleDES.encrypt('4111111111111111', key, { iv: iv });
    
    var result = encrypted.toString();
    alert('Encrypted Value: ' + result);    
    $("#encryptSpan").text(result);  
    
    var decrypted = CryptoJS.TripleDES.decrypt(encrypted, key, { iv: iv }).toString(CryptoJS.enc.Utf8);
    
    alert('Decrypted Value: ' + decrypted);
    $("#decryptSpan").text(decrypted); 
    
}