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 CPOL
Rate this:
Please Sign up or sign in to vote.
Use JavaScript to write AES encryption and decryption methods


  • 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):

Using the Code

  1. Within an HTML page, include a reference to the Google code AES JavaScript file:
    <script src=""></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);
        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);
  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'>
    <textarea name='cryptoff' id='cryptoff' cols=40 rows=5 onkeyup=\"encode(;\">
    Type here to encrypt..</textarea>
    <textarea name='crypton' id='crypton' cols=40 rows=5 onkeyup=\"decode(;\">
    Ciphertext here to decrypt..

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


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
Web03 | 2.8.141022.2 | Last Updated 19 Mar 2014
Article Copyright 2014 by The Operator
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid