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

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

, 12 Nov 2014 CPOL
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 (Experiments -> Cryptography (JS))

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&middot;off:
    <textarea name='cryptoff' id='cryptoff' cols=40 rows=5 onkeyup=\"encode(this.id);\">
    Type here to encrypt..</textarea>
    <br>crypt&middot;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)

Share

About the Author

The Operator
Oproot Research
United States United States
+
 

Oproot Research
Research, Development
Science Experiments
Photography, Reading

Comments and Discussions

 
GeneralGreat PinmemberEngrBS17-Feb-15 19:25 
GeneralMy vote of 5 PinmemberChinmoy Mohanty24-Nov-14 9:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 12 Nov 2014
Article Copyright 2014 by The Operator
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid