Question: I am trying to make a simple chat action where me is the first user and you is the second user. All I am trying to put input text into `ul li`. condition if last `ul` has class me then alert me else alert you. Kindly advice.

Following is my code.

**HTML**

```
<div class="chat-left">
    <div class="msg-area">
        <ul class="me">
            <li>what is this msg</li>
        </ul>
        <ul class="you">
            <li>others message</li>
        </ul>
    </div>
    <form id="me">
        <input type="text" name="mymsg" />
        <input type="submit" class="submit" name="submit" />
    </form>
</div>

```

**jQuery**

```
$(document).ready(function () {
    $('#me').submit(function (event) {
        event.preventDefault();
        console.log('left');
        var $mymsg = $('input[type="text"]').val();
        $(this).siblings('.msg-area ul').each(function () {
            if ($(this).hasClass('me')) {
                console.log('me');
            }
        });
    });
});

```

Answer: 
Your "password" (it is used as a key instead of a password) has two different encodings on client-side and server-side. On the client, you're parsing it as Hex, but on the server, you're passing it as a binary string, which is used as-is.

You either need parse it on the server (now it is AES-128 and not AES-256):

```
pass: new Buffer("0123456789abcdef0123456789abcdef", "hex"),

```

Or change the encoding on the client (from AES-128 to AES-256):

```
pass: CryptoJS.enc.**Utf8**.parse("0123456789abcdef0123456789abcdef"),
```

---

```
  encript: function (msg) {
    try {
      var options = { mode: CryptoJS.mode.CBC, iv: this.iv};
      var json = CryptoJS.AES.encrypt(msg, this.pass, options);
      return json.ciphertext.toString(CryptoJS.enc.Hex);
    } catch (err) {
      console.error(err);
      return "";
    }
  },
  decript: function (hex){
    try {
      var options = { mode: CryptoJS.mode.CBC, iv: this.iv};
      var json = CryptoJS.AES.decrypt({
        ciphertext: CryptoJS.enc.Hex.parse(hex)
      }, this.pass, options);
      return json.toString(CryptoJS.enc.Utf8);
    } catch (err) {
      console.error(err);
      return "";
    }
  }

```

---

You still have security problems:

* The IV has to be randomly chosen for every encryption under the same key to achieve semantic security. It doesn't have to be secret, so you can simply send it along with the ciphertext. It's common to prepend it to the ciphertext and slice it off before decryption.
* It is better to authenticate your ciphertexts so that attacks like a [padding oracle attack](https://crypto.stackexchange.com/q/18185/13022) are not possible. This can be done with authenticated modes like GCM or EAX, or with an [encrypt-then-MAC](https://crypto.stackexchange.com/q/202/13022) scheme with a strong MAC like HMAC-SHA256, which CryptoJS provides.
Here is the example node js crypto ( <https://nodejs.org/api/crypto.html>) and related encryption and decryption in crypto.js (<https://github.com/brix/crypto-js>).

Gist - <https://gist.github.com/yoavniran/c78a0991e0152b306c25>

```
//CryptoJS example
var key = "H98zM6i/55yNJfkFsbu0HrzlFo17FtR9";
var iv = key.slice(0, 16);
//Create Key
key = CryptoJS.enc.Utf8.parse(key);
//Get Iv
iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt("testtest", key,{ iv: iv});
//Encrypt string
var encrypted_data = encrypted.toString();
console.log(encrypted_data)
var decrypted = CryptoJS.AES.decrypt(encrypted_data, key,{ iv: iv});
var decrypted_data = decrypted.toString(CryptoJS.enc.Utf8)
console.log(decrypted_data)

////Node JS example
  var key = "H98zM6i/55yNJfkFsbu0HrzlFo17FtR9";
  var iv = key.slice(0, 16);
  function encryptText(cipher_alg, key, iv, text, encoding) {
      var cipher = crypto.createCipheriv(cipher_alg, key, iv);
      encoding = encoding || "binary";
      var result = cipher.update(text,'utf8',  encoding);
      result += cipher.final(encoding);
      return result;
  }
  function decryptText(cipher_alg, key, iv, text, encoding) {
      var decipher = crypto.createDecipheriv(cipher_alg, key, iv);
      encoding = encoding || "binary";
      var result = decipher.update(text, encoding);
      result += decipher.final();
      return result;
  }
var encText = encryptText('aes256', key, iv, "testtest1", "base64");
console.log("encrypted text = " + encText);
var decText = decryptText('aes256', key, iv, encText, "base64");
console.log("decrypted text = " + decText);

```