Crypto.JS - DES

by Mohit Agrawal

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>

JavaScript

function encryptByDES(message, key) {
	key = CryptoJS.MD5(key);

	// copy 3DES subkey 1 to the last 64 bit to make a full 192-bit key
	key.words[4] = key.words[0];
	key.words[5] = key.words[1];

	// create a 64-bit zero filled
	var iv = CryptoJS.lib.WordArray.create(64/8);
	var encrypted = CryptoJS.TripleDES.encrypt(message, key, {iv: iv});
	return encrypted.toString();
};

function decryptByDES(encryptedBase64, key) {
	key = CryptoJS.MD5(key);

	// copy 3DES subkey 1 to the last 64 bit to make a full 192-bit key
	key.words[4] = key.words[0];
	key.words[5] = key.words[1];

	// create a 64-bit zero filled
	var iv = CryptoJS.lib.WordArray.create(64/8);
	
	var ct = { ciphertext: CryptoJS.enc.Base64.parse(encryptedBase64) };
	var decrypted = CryptoJS.TripleDES.decrypt(ct, key, {iv: iv});
	return decrypted.toString(CryptoJS.enc.Utf8)
};

var key = 'Bsl!ldap2014';
var ADID = 'INOS007445';
var PASSWORD = 'Sep@2020';

var encryptedID = encryptByDES(ADID, key);
var encryptedPass = encryptByDES(PASSWORD, key);

console.log("encryptedID: ", encryptedID);
console.log("encryptedPass: ", encryptedPass);

var decryptedID = decryptByDES(encryptedID, key);
var decryptedPass = decryptByDES(encryptedPass, key);

console.log("decryptedID: ", encryptedID);
console.log("decryptedPass: ", encryptedPass);