Search code examples
javascriptencryptionaescryptojs

JS libraries required for CryptoJS Encryption


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

Solution

  • 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);
    
    });