Click here to Skip to main content
12,450,766 members (49,285 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

26.3K views
15 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAES Encryption to encrypt a file uploaded Pin
satia9310-Apr-16 4:04
membersatia9310-Apr-16 4:04 
GeneralGreat Pin
EngrBS17-Feb-15 18:25
memberEngrBS17-Feb-15 18:25 
GeneralMy vote of 5 Pin
Chinmoy Mohanty24-Nov-14 8:16
memberChinmoy Mohanty24-Nov-14 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web01 | 2.8.160826.1 | Last Updated 12 Nov 2014
Article Copyright 2014 by The Operator
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid