AES encryption using JavaScript

The Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. NIST in 2001. This encryption algorithm is used for securing sensitive, but unclassified material by U.S. Government agencies. AES has been widely adopted by enterprise and has become a de facto encryption standard for many commercially sensitive transactions.

This recipe discusses how AES can be implemented in JavaScript and incorporated into your Titanium Enterprise app.

Getting ready

This recipe uses the Ti.SlowAES CommonJS module as a wrapper around the SlowAES open source project. This module and other code assets can be downloaded from the source provided by the book, or individually through the links provided in the See also section at the end of this recipe. Installing these in your project is straightforward. Simply copy the SlowAES folder into the Resources folder of your project as shown in the following screenshot:

Getting ready

How to do it...

Once you have added the SlowAES folder to your project, next you need to create your application namespace in the app.js file and use require to import the module into your code as the following code snippet demonstrates:

//Create our application namespace
var my = {
  mod : require('SlowAES/Ti.SlowAES')
};

Creating the recipe UI

This recipe demonstrates the usage of the Ti.SlowAES CommonJS module through a sample app using two Ti.UI.TextField controls for input.

  1. First, a Ti.UI.Window is created to attach all UI elements.
    var win = Ti.UI.createWindow({
      backgroundColor: '#fff', title: 'AES Crypto Example',
      barColor:'#000',layout:'vertical',fullscreen:false
    });
    Creating the recipe UI
  2. Next, a Ti.UI.TextField control is added to the Ti.UI.Window to gather the secret from the user.
    var txtSecret = Ti.UI.createTextField({
      value:'DoNotTell',hintText:'Enter Secret',
      height:45, left:5, right:5,
      borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED
    });
    win.add(txtSecret);
  3. Another Ti.UI.TextField is added to the Ti.UI.Window to gather the string to encrypt from the user.
    var txtToEncrypt = Ti.UI.createTextField({
      value:'some information we want to encrypt',
      hintText:'Enter information to encrypt',
      height:45, left:5, right:5,
      borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED
    });
    win.add(txtToEncrypt);
  4. Next a Ti.UI.Label is added to the Ti.UI.Window. This Ti.UI.Label will be used to display the encrypted value to the user.
    var encryptedLabel = Ti.UI.createLabel({
      top:10, height:65, left:5, right:5,color:'#000',
      textAlign:'left',font:{fontSize:14}
    });
    win.add(encryptedLabel);
  5. Finally a Ti.UI.Button is added to the Ti.UI.Window. This Ti.UI.Button will be used later in the recipe to perform the encryption test.
    var btnEncrypt = Ti.UI.createButton({
      title:'Run Encryption Test', top:25, 
      height:45, left:5, right:5
    });
    win.add(btnEncrypt);

Encrypting and decrypting values

This section demonstrates how to use the Ti.SlowAES module to use the secret entered in the txtSecret, Ti.UI.TextField to encrypt the contents of the txtToEncrypt, Ti.UI.TextField. Once completed, the encrypted value is then decrypted and compared against the original input. The results are displayed to the user in an alert message as shown in the following screenshots:

Encrypting and decrypting values

The encryption test is performed when the click event for the btnEncrypt control is fired as shown in the following code snippet:

btnEncrypt.addEventListener('click',function(x){
  1. The first step in the encryption process is to create a new instance of the SlowAES module as shown in this code snippet.
      var crypto = new my.mod();
  2. Next using the encrypt function, the secret provided in the txtSecret control is used to encrypt the value in the txtToEncrypt control. The encrypted results are then returned to the encryptedValue as demonstrated in the following statement:
      var encryptedValue = 
      crypto.encrypt(txtToEncrypt.value,txtSecret.value);
  3. The encryptedLabel.text property is then updated to display the encrypted value to the user.
      encryptedLabel.text = 'Encrypted:' + encryptedValue;
  4. Next, the decrypt method is used to demonstrate how to decrypt the string value encrypted earlier. This method requires the encrypted string value and the secret as shown in the following snippet:
      var decryptedValue = 
      crypto.decrypt(encryptedValue,txtSecret.value);
  5. Finally, the original input value is compared against the decrypted value to ensure our encryption test was successful. The results of this test are then displayed to the user through a message alert and the Titanium Studio console.
      alert((txtToEncrypt.value ===decryptedValue) ? 
      'Encryption Test successfully ran check console for details.': 
      'Test failed, please check console for details.'),
    
    });

See also

You can also refer to the following resources:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.108.111