AES encryption with CryptoJS decomposing unicode emoji

I am writing a system where the user can write something (via a mobile browser), and that "String" will be encrypted using the password selected by the user. Because unicode emojis are often used, they must also be supported.

As the lib for cryptography, I choose CryptoJs so that cryptography can be done locally on devices.

Currently, when I encrypt a string and decrypt the same, all emojis disappear / are replaced by random characters.

var key = "123";
var content = "secret text with an emoji, 🎮";

var encrypted = aes_encrypt(key, content); //U2FsdGVkX19IOHIt+eRkaOcmNuZrc1rkU7JepL4iNdUknzhDaLOnSjYBCklTktSe

var decrypted = aes_decrypt(key, encrypted);//secret text with an emoji, Ø<ß®

I use a couple of helper functions, such as:

function aes_encrypt(key, content){
  var key_string = key + "";
  var content_string = ascii_to_hex(content) + "";
  var key_sha3 = sha3(key_string);
  var encrypted = CryptoJS.AES.encrypt(content_string, key_sha3, {
      mode: CryptoJS.mode.CTR, padding: CryptoJS.pad.Iso10126});
  return encrypted + "";
};

Can someone please tell me what I am doing wrong?

+4
source share
2

: . JavaScript, ( ) . CryptoJS, , .

, - , .

JavaScript , . ( Blobs TypedArrays Buffers Node.js), - , .

( atob btoa) - . JavaScript ( Unicode, UCS-2/UTF-16). , , , .

, ASCII, , (.. , ). , , ASCII, , , UTF-16, . , .

( ) , UTF-8 . JavaScript . , encodeURIComponent UTF-8 URL-, ASCII. - :

var key = "123";
var content = "secret text with an emoji, 🎮";

var encrypted = aes_encrypt(key, encodeURIComponent(content));

var decrypted = decodeURIComponent(aes_decrypt(key, encrypted));

, URL-, , , , . , encodeURIComponent, -, , " ". , , - .

, CryptoJS , . , .

+6

CryptoJS UTF-8 (WordArray). var binData = CryptoJS.enc.Utf8.parse(string);:

var password = "123";
var content = "secret text with an emoji, 🎮";

inContent.innerHTML = content;

var encrypted = aes_encrypt(password, content);
var decrypted = aes_decrypt(password, encrypted);

out.innerHTML = decrypted;

function aes_encrypt(password, content) {
  return CryptoJS.AES.encrypt(content, password).toString();
}

function aes_decrypt(password, encrypted) {
  return CryptoJS.AES.decrypt(encrypted, password).toString(CryptoJS.enc.Utf8);
}
#inContent { color: blue; }
#out { color: red; }    
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/aes.js"></script>
<div>in: <span id="inContent"></span></div>
<div>out: <span id="out"></span></div>

, , CryptoJS.AES.encrypt, UTF-8, UTF-8 . .toString(CryptoJS.enc.Utf8).


, CryptoJS UTF-8 . ,

+2

Source: https://habr.com/ru/post/1629635/


All Articles