1

I am trying to use CryptoJS for encryption using the code snippets from an upvoted answer of CryptoJS AES encryption and Java AES decryption.

var text = "The quick brown fox jumps over the lazy dog.  ";
var secret = "René Über";
var encrypted = CryptoJS.AES.encrypt(text, secret);
encrypted = encrypted.toString();
console.log("Cipher text: " + encrypted);

But it does not mention how to include the JS files required for this. I tried using the below links to include them.

'aes':'http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes',
'enc-base64-min':'http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min'

But it gives an error "Refused to execute script from because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."

Can some one tell me how do I get the scripts executed, any other URL to be included or is it possible to manually include these js files.

Here is how my main.js looks like

requirejs.config({
    // Path mappings for the logical module names
    paths: {
        'knockout': '../../js/libs/knockout/knockout-3.4.0',
        'jquery': '../../js/libs/jquery/jquery-1.9.1',
        'jqueryui': '../../js/libs/jquery/jquery-ui-1.10.4.custom',
        'ojs': '../../js/libs/oj/v3.2.0/min',
        'ojL10n': '../../js/libs/oj/v3.2.0/ojL10n',
        'ojtranslations': '../../js/libs/oj/v3.2.0/resources',
        'text': '../../js/libs/require/text',
        'jqueryui-amd': '../../js/libs/jquery/jqueryui-amd-1.12.0',
        'promise': '../../js/libs/es6-promise/es6-promise',
        'hammerjs': '../../js/libs/hammer/hammer-2.0.8',
        'ojdnd': '../../js/libs/dnd-polyfill/dnd-polyfill-1.0.0',
        'signals': '../../js/libs/js-signals/signals',
        'customElements': '../../js/libs/webcomponents/CustomElements',
        'proj4': '../../js/libs/proj4js/dist/proj4-src',
        'css': '../../js/libs/require-css/css',
        'crypto-js': 'crypto-js/crypto-js-develop'
    },// Shim configurations for modules that do not expose AMD
    shim: {
        'jquery': {
            exports: ['jQuery', '$']
        },
        'jqueryui': {
            deps: ['jquery']
        }
    },
    config: {
        ojL10n: {
            merge: {  
            }
        }
    },
    catchError: true
});

require([ 'ojs/ojcore', 'knockout', 'jquery', 'commonController', 'ojs/ojknockout','ojs/ojmodule','ojs/ojcomponents',
        'ojs/ojaccordion', 'ojs/ojcollapsible', 'ojs/ojselectcombobox', 'ojs/ojtoolbar',
        'ojs/ojprogressbar', 'ojs/ojinputnumber', 'ojs/ojrouter', 'ojs/ojtable', 'ojs/ojarraytabledatasource','promise','ojs/ojinputtext','crypto-js'], 
         function(oj, ko, $, commonController) {


    $(function() {

        function init() {
            oj.Router.sync().then(function() {

                ko.applyBindings(commonController, document.getElementById('globalBody'));
            }, function(error) {
                oj.Logger.error('Error in root start: ' + error.message);
                alert(error);
            });
        }

        init();
    });
});

requirejs.onError = function (err){
    //alert(err);
};
Arunabh
  • 167
  • 1
  • 7
  • 18
  • Both the URL you are using are 404! That's why you are being served a plain HTML page in the first place... – Salketer Oct 23 '17 at 12:27
  • Looking at your edit, my guess is that the crypto-js folder is wrong... Isn't it sitting next to the other packages? – Salketer Oct 23 '17 at 14:03

1 Answers1

3

The AES and base64 are parts of CryptoJS already. No need to include more scripts.

From the project's github page:

var CryptoJS = require("crypto-js");

// 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);

To use with requireJS:

require.config({
    packages: [
        {
            name: 'crypto-js',
            location: 'path-to/bower_components/crypto-js',
            main: 'index'
        }
    ]
});

You'll need to change the location to the path of the crypto-js distribution. Also, you should change your use of cryptoJS like so

require(["crypto-js"], function (CryptoJS) {
    // 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);

});
Salketer
  • 14,263
  • 2
  • 30
  • 58
  • But how do I include CryptoJS? – Arunabh Oct 23 '17 at 12:29
  • I tried using var CryptoJS = require("crypto-js"); but then at line CryptoJS.AES.encrypt CryptoJS comes out to be undefined which means the js file could not be loaded. Can you please help. – Arunabh Oct 23 '17 at 12:55
  • Is that code running on a NodeJS server or inside a browser? – Salketer Oct 23 '17 at 13:49
  • This code is inside a javascript file running on a browser. I downloaded complete folder from https://github.com/brix/crypto-js and placed it inside my workspace. But not quite sure how do I include it in the project. I am using RequireJS for inclusion of libraries. – Arunabh Oct 23 '17 at 13:56
  • In the main.js file which has require function I have given the path of this newly downloaded folder. Edited my question giving main.js file contents. – Arunabh Oct 23 '17 at 13:57
  • I am trying this but can't see any file as 'index.js' inside the Crypto folder. Which js file do I point it to? – Arunabh Oct 24 '17 at 07:28
  • You seem to be using the develop branch, you should get the master branch that's the one packing the distribution version. – Salketer Oct 24 '17 at 07:33
  • If you use code like `CryptoJS.AES.encrypt('my message', 'secret key 123')` in front-end, it is not actually encrypted because people can see your `secret key`, with a few rows of code they can decrypt what you want to hide. Is there any Asymmetric encryption method using RSA keys? – Wayne Wei Nov 13 '19 at 09:22
  • Use JSEncrypt if that's what you want. https://github.com/travist/jsencrypt – Salketer Nov 14 '19 at 14:19