Encryption - Decryption In JavaScript Like A Student Thought

A student thought

An algorithm may be similar to basic algorithms of Information Security books.

I'm thankful to my colleague Mr.Jayesh Patel and my cousin Maulik Trivedi who is currently a student of computer engineering department at Marwadi College of Engineering and Technology, Rajkot.

I will describe an algorithm with practical steps proven with JavaScript code or you can perform it in any language it's your choice.

Graphic layout of tool works on this algorithm and depends on your thoughts.

I am designing a webpage like the following screenshot,

designing

Description of algorithm

A simple algorithm works with & play with an ASCII value of characters. Algorithm works within the following six different steps.

Step 1: Convert input string characters in respected ASCII codes & store it in array like the following example of JavaScript code.

  1. for(i = 0; i < inputString.length; i++)  
  2. {  
  3.     asciiArr[i] = inputString[i].charCodeAt(0);  
  4. }  
Step 2: Fill A to Z array in capital or small letters:
  1. for(i = 0, code = 65; i < 26; i++, code++)  
  2. {  
  3.     atozArr[i] = String.fromCharCode(code);  
  4. }  
Step 3: Choose randomly single character index from A to Z and differentiate it's position in one variable & its respected ASCII value in second variable.

Note: Here, I am referencing a function returning a random value from minimum - maximum range. Write a function outside the main function of your code.
  1. position = randomIndexFromInterval(0, atozArr.length - 1);  
  2. positionAscii = atozArr[position].charCodeAt(0);  
  3.   
  4. function randomIndexFromInterval(min, max)  
  5. {  
  6.     return Math.floor(Math.random() * (max - min + 1) + min);  
  7. }  
Step 4: Addition of every input String each element to positionAscii.
  1. for(i = 0; i < inputString.length; i++)  
  2. {  
  3.     encryptedString[i] = parseInt(asciiArr[i]) + parseInt(atozArr[position].charCodeAt(0));  
  4. }  
Step 5: Attachment of key to encrypted string or not it's your choice & it's your modification scenario. Here I am attaching a key with an encrypted string.

Step 6: Finally your encryption is ready to send.
  1. for(i = 0; i < encryptedString.length; i++)  
  2. {  
  3.     outputString.innerHTML = outputString.innerHTML + String.fromCharCode(encryptedString[i]);  
  4. }  
Here outputString is an html element to display output on a webpage. For decryption process reverse steps apply.

Plain Text:

Hi I am writing a blog on blogspot.com.

Cypher Text:

¾u?u¶ÂuÌǾɾüu¶u·ÁļuÄÃu·ÁļÈÅÄÉ?¸ÄÂU

Note: Every time cypher text will change due to selection of random position from A to Z.

So guys here is an output screenshot of my web browser Mozilla Firefox.

Cypher Text

JavaScript Code for Practice

 

  1. function encryption()  
  2. {  
  3.     var inputString = document.getElementById("input")  
  4.         .value;  
  5.     var outputString = document.getElementById("enc");  
  6.     var asciiArr = new Array();  
  7.     var atozArr = new Array();  
  8.     var encryptedString = new Array();  
  9.     if(inputString.length != 0)  
  10.     {  
  11.         outputString.innerHTML = "";  
  12.         //First Step: Convert all characters in ascii code     
  13.         for(i = 0; i < inputString.length; i++)  
  14.         {  
  15.             asciiArr[i] = inputString[i].charCodeAt(0);  
  16.         }  
  17.         //Second Step: Fill AtoZ array in capital or small letters     
  18.         for(i = 0, code = 65; i < 26; i++, code++)  
  19.         {  
  20.             atozArr[i] = String.fromCharCode(code);  
  21.         }  
  22.         //Third Step: Choose random single character index from A to Z    
  23.         position = randomIndexFromInterval(0, atozArr.length - 1);  
  24.         positionAscii = atozArr[position].charCodeAt(0);  
  25.         //Fourth Step: Addition of every inputString element to positionAscii    
  26.         for(i = 0; i < inputString.length; i++)  
  27.         {  
  28.             encryptedString[i] = parseInt(asciiArr[i]) + parseInt(atozArr[position].charCodeAt(0));  
  29.         }  
  30.         //Fifth Step: Attach key to encrypted string    
  31.         encryptedString[asciiArr.length] = positionAscii;  
  32.         //Sixth Step: Finally your encryption is ready to send    
  33.         for(i = 0; i < encryptedString.length; i++)  
  34.         {  
  35.             outputString.innerHTML = outputString.innerHTML + String.fromCharCode(encryptedString[i]);  
  36.         }  
  37.         document.getElementById("inputBox")  
  38.             .value = outputString.innerHTML;  
  39.     }  
  40.     else  
  41.     {  
  42.         document.getElementById("enc")  
  43.             .innerHTML = "Error: Value can not be empty.";  
  44.         return false;  
  45.     }  
  46. }  
  47.   
  48. function decryption()  
  49. {  
  50.     var inputBox = document.getElementById("inputBox");  
  51.     var plainText = document.getElementById("dec");  
  52.     if(inputBox.value != 0)  
  53.     {  
  54.         var encryptedString = inputBox.value;  
  55.         var key = encryptedString[encryptedString.length - 1];  
  56.         var decryptedString = new Array();  
  57.         for(i = 0; i < encryptedString.length - 1; i++)  
  58.         {  
  59.             decryptedString[i] = encryptedString[i].charCodeAt(0) - key.charCodeAt(0);  
  60.         }  
  61.         plainText.innerHTML = "";  
  62.         for(i = 0; i < decryptedString.length; i++)  
  63.         {  
  64.             plainText.innerHTML = plainText.innerHTML + String.fromCharCode(decryptedString[i]);  
  65.         }  
  66.     }  
  67.     else  
  68.     {  
  69.         document.getElementById("dec")  
  70.             .innerHTML = "Error: Value can not be empty.";  
  71.         return false;  
  72.     }  
  73. }  
  74.   
  75. function randomIndexFromInterval(min, max)  
  76. {  
  77.     return Math.floor(Math.random() * (max - min + 1) + min);  
  78. }  

 

online shop

Up Next
    Ebook Download
    View all
    Learn
    View all