17

I used CryptoJS instead of nodejs crypto module because I just use the native JavaScript,but some codes can't work:

function aesEncrypt(text, secKey) {
  const _text = text
  const lv = new Buffer('0102030405060708', 'binary')
  const _secKey = new Buffer(secKey, 'binary')
  const cipher = crypto.createCipheriv('AES-128-CBC', _secKey, lv)
  let encrypted = cipher.update(_text, 'utf8', 'base64')
  encrypted += cipher.final('base64')
  return encrypted
}

So how should I modify these codes?

Melchia
  • 22,578
  • 22
  • 103
  • 117
zhaowweny
  • 241
  • 1
  • 2
  • 6

3 Answers3

29

Here's a sample on how to use CryptoJs in webclient:

// INIT
var myString   = "blablabla Card game bla";
var myPassword = "myPassword";

// PROCESS
var encrypted = CryptoJS.AES.encrypt(myString, myPassword);
var decrypted = CryptoJS.AES.decrypt(encrypted, myPassword);
document.getElementById("demo0").innerHTML = myString;
document.getElementById("demo1").innerHTML = encrypted;
document.getElementById("demo2").innerHTML = decrypted;
document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
</head>


  
  <strong><label>Original String:</label></strong>
  <span id="demo0"></span>

  <br>
  <br>
  
  <strong><label>Encrypted:</label></strong>
  <span id="demo1"></span>

  <br>
  <br>
  
  <strong><label>Decrypted:</label></strong>
  <span id="demo2"></span>

  <br> 
  <br>

  <strong><label>String after Decryption:</label></strong>
  <span id="demo3"></span>

  
  <br />
  <br />
 

NB:

You might want to use CDN if you don't want to use node modules.

Community
  • 1
  • 1
Melchia
  • 22,578
  • 22
  • 103
  • 117
  • thank you very much, but how to use createCipheriv function , CryptoJS doesn't seems to have that function. – zhaowweny Jun 23 '18 at 23:01
  • I don't think there is function called createCipheriv in github.com/brix/crypto-js – Melchia Jun 23 '18 at 23:17
  • Yes, I think so. So I want to modify those codes to implement the same function. – zhaowweny Jun 23 '18 at 23:20
  • https://github.com/brix/crypto-js/search?q=createCipheriv&unscoped_q=createCipheriv – Melchia Jun 23 '18 at 23:35
  • What if the JS is in a different file from the html? This example does not tie together the js with the html. I get a CryptoJS not found. – WestCoastProjects Jul 14 '20 at 23:45
  • 1
    I don't understand your question. Everything is done using CryptoJs library. I merely used the html here to download the library for the online demo. If I were to use it in a NodeJs project I would install it directly using npm install crypto-js – Melchia Jul 14 '20 at 23:59
  • I suggest you open a question explaining the details of your case – Melchia Jul 15 '20 at 00:02
  • good answer, but you must use toString func to show encrypted string: document.getElementById("demo1").innerHTML = encrypted.toString(); – Eyni Kave Jun 21 '21 at 07:03
10

How about CryptoJS?

It's a solid crypto library, with a lot of functionality. It implements hashers, HMAC, PBKDF2 and ciphers. In this case ciphers is what you need. Check out the quick-start quide on the project's homepage.

 var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
//U2FsdGVkX18ZUVvShFSES21qHsQEqZXMxQ9zgHy+bu0=

var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
//4d657373616765


document.getElementById("demo1").innerHTML = encrypted;
document.getElementById("demo2").innerHTML = decrypted;
document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
Full working sample actually is:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

<br><br>
<label>encrypted</label>
<div id="demo1"></div>
<br>

<label>decrypted</label>
<div id="demo2"></div>

<br>
<label>Actual Message</label>
<div id="demo3"></div>
Murtaza Hussain
  • 3,851
  • 24
  • 30
0

Just for compelete previouse answers, you must use toString function to show the result as string because encrypt and decrypt functions returns object:

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
console.log(encrypted.toString());
console.log(decrypted.toString());
console.log(decrypted.toString(CryptoJS.enc.Utf8));
Eyni Kave
  • 1,113
  • 13
  • 23