Click here to Skip to main content
Click here to Skip to main content

Cryptography: Client-side AES Encryption Using Google Javascript Crypto Library

, 19 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Use JavaScript to write AES encryption and decryption methods

Introduction

  • Cryptography: Encryption is the process of encoding plaintext into an unreadable ciphertext. The encryption key (secret key) specifies how a message is to be encoded.
  • Authorized: Able to decode the ciphertext using the decryption algorithm that requires the secret key.
  • Unauthorized: Must not have access to the key. By viewing the ciphertext, the unauthorized should not be able to determine anything about the original plaintext.
  • Demo (crypt-on): http://oproot.com/

Using the Code

  1. Within an HTML page, include a reference to the Google code AES JavaScript file:
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> 
  2. Next, add the encryption and decryption methods:
        <script type="text/javascript">  
            
        function encode(control)
        {
            var ckey = $("#cryptonkey").val();
            var y=document.getElementById(control).value;
            var encrypted = CryptoJS.AES.encrypt(y, ckey);
            document.getElementById("crypton").value=encrypted;
        }
    
        function decode(control)
        {
            var ckey = $("#cryptonkey").val();
            var y=document.getElementById(control).value;
            var decrypted = CryptoJS.AES.decrypt(y, ckey); 
            decrypted = decrypted.toString(CryptoJS.enc.Utf8);
            document.getElementById("cryptoff").value=decrypted;
        } 
        </script> 
  3. Finally, add the HTML controls that pass the values to be encrypted/decrypted to the JavaScript methods:
    create secret key: 
    <input id='cryptonkey' value='123456'>
    
    crypt·off:
    <textarea name='cryptoff' id='cryptoff' cols=40 rows=5 onkeyup=\"encode(this.id);\">
    Type here to encrypt..</textarea>
    <br>crypt·on:<br>
    <textarea name='crypton' id='crypton' cols=40 rows=5 onkeyup=\"decode(this.id);\">
    Ciphertext here to decrypt..
    </textarea>

When a user types into the cryptoff textarea, the text will be encrypted and displayed in the "crypton" textarea.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

The Operator

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 19 Mar 2014
Article Copyright 2014 by The Operator
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid