Change the Key with CryptoJS [duplicate]

I am using CryptoJS to encrypt and decrypt the text. Here, I am just taking the message and showing the both encryption and decryption messages.

I am using DES algorithm for encrypting and decrypting.

This is my HTML file

<!DOCTYPE html>
    <script src="tripledes.js"></script>
    <script src="mode-ecb.js"></script>
    <style type="text/css">
        .maindiv {
            /* Just to center the form on the page */
            margin: 0 auto;
            width: 400px;
            /* To see the outline of the form */
            padding: 1em;
            border: 1px solid #CCC;
            border-radius: 1em;
         div + div {
                margin-top: 1em;
        label {
            /* To make sure that all labels have the same size and are properly aligned */
            display: inline-block;
            width: 90px;
            text-align: right;
        .button {
            /* To position the buttons to the same position of the text fields */
            padding-left: 90px; /* same size as the label elements */

        button {
            /* This extra margin represent roughly the same space as the space
       between the labels and their text fields */
            margin-left: .5em;
        input:focus, textarea:focus {
            /* To give a little highlight on active elements */
            border-color: #000;
        input, textarea {
            /* To make sure that all text fields have the same font settings
       By default, textareas have a monospace font */
            font: 1em sans-serif;
            /* To give the same size to all text field */
            width: 300px;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            /* To harmonize the look & feel of text field border */
            border: 1px solid #999;
    <script type="text/javascript">

        function viewvalue()
            var message = document.getElementById("msg").value;
            var key = document.getElementById("key").value;
            var encrypted = encryptByDES(message, key);
            document.getElementById("enctext").textContent = encrypted;
            document.getElementById("dectxt").textContent = decryptByDES(encrypted, key);;


        function encryptByDES(message, key) {

            var keyHex = CryptoJS.enc.Utf8.parse(key);

            var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            return encrypted.toString();

        function decryptByDES(ciphertext, key) {
            var keyHex = CryptoJS.enc.Utf8.parse(key);

            var decrypted = CryptoJS.DES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
            }, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7

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

    <div class="maindiv">
            <label for="name">Message:</label>
            <input type="text" id="msg" name="msg" />
            <label for="mail">Key:</label>
            <input type="text" id="key" name="key" />
            <label for="msg">Encrypted Text:</label>
            <textarea id="enctext" name="enctxt"></textarea>
            <label for="msg">Decrypted Text:</label>
            <textarea id="dectxt" name="dectxt"></textarea>
        <div class="button">
            <button onclick="viewvalue()">View</button>

This is my .js file

CryptoJS v3.1.2
(c) 2009-2013 by Jeff Mott. All rights reserved.
 * Electronic Codebook block mode.
CryptoJS.mode.ECB = (function () {
    var ECB = CryptoJS.lib.BlockCipherMode.extend();

    ECB.Encryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.encryptBlock(words, offset);

    ECB.Decryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.decryptBlock(words, offset);

    return ECB;

Please anyone can tell me where and how to change the key.



According to the docs at, you would need to define and supply both the initialisation vector (IV) and the key if you wish to provide a custom key:

var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'); 
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f'); 
var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv });


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.