5

I want to encrypt and decrypt string in javascript using following code.But i got crypto js is not defined.

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

4
  • 2
    Is the script tag for cryptojs before your javascript? Commented May 21, 2018 at 12:55
  • you need to convert it to tostring Commented May 21, 2018 at 12:57
  • I placed your logic in a snippet where it appears to work (although the response appears to be objects not strings). Check to ensure you're including the crypto script in the page before you run your own logic. Commented May 21, 2018 at 12:58
  • You're not working with the responses from the method calls properly. See the AES section of the documentation about 2/3 of the way down this page: github.com/brix/crypto-js Commented May 21, 2018 at 13:00

3 Answers 3

11

Works fine for me.

JSFiddle: https://jsfiddle.net/xbk736br/

// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');

// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);

console.log(plaintext);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

4

If you are building a react or nodejs app, you can simply use this library ncrypt-js to encrypt and decrypt your data.

See example on codesandbox

usage:

es5

var ncrypt = require('ncrypt-js'); // or var { encrypt, decrypt } = require('ncrypt-js);

let encryptData = ncrypt.encrypt('super secret data', 'secret_key');
// or
// let encryptData = encrypt('super secret data', 'secret_key');

console.log(encryptData); // 11ab949601eb136f58ac3fe846e30d76.f9ce133b20adc35eef32af95957547abbb6fbfc5cb91cd14f5b0a088bd031883963cde1a56fd62fe2aeb75451a065d21
var decryptedData = ncrypt.decrypt(encryptData);
// or
// var decryptedData = decrypt(encryptData);

console.log(decryptedData); // super secret data

es6

import ncrypt from 'ncrypt-js'; // or import { encrypt, decrypt } from 'ncrypt-js';

const encryptData = ncrypt.encrypt('super secret data', 'secret_key');
// or
// const encryptData = encrypt('super secret data', 'secret_key');

console.log(encryptData); // 11ab949601eb136f58ac3fe846e30d76.f9ce133b20adc35eef32af95957547abbb6fbfc5cb91cd14f5b0a088bd031883963cde1a56fd62fe2aeb75451a065d21
const decryptedData = ncrypt.decrypt(encryptData);
// or
// const decryptedData = decrypt(encryptData);

console.log(decryptedData); // super secret data

Comments

2

encrypted.toString() will return the correct string value.

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
alert(encrypted.toString());
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

2 Comments

while encrypting? I am not getting any error actually.
Double check whether your code is placed correctly in <script> tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.