Tuesday 22 March 2016

Caesar Cipher program in JavaScript with slight modification

Encryption Like Student thought

 

Hi all engineering students and professionals who are currently interested in information security related topics.

I am posting an algorithm which based is Caesar Cipher.

An algorithm with slight modification with Caesar Cipher.

 

Introduction to Caesar Cipher

The earliest known use of a substitution cipher, and the simplest, was by Julius Caesar.

The Caesar cipher involves replacing each letter of the alphabet with the letter standing three places further down the alphabet.

For ex.

plain:  meet me after the toga party 

cipher: PHHW PH DIWHU WKH WRJD SDUWB

  

Modification is new thing - like student thought

A slight modification with the key used in Caesar Cipher.

An practical steps of algorithm modification is done with JavaScript Code

An algorithm works with only 7th step.

It's very easy to implement in programming languages you interested, here i am implementing this seven steps in JavaScript language.

Graphic layout of tool works on this algorithm is depends on your thoughts.
I am designing a webpage like this.


Step - 1

Convert input string characters in respected ascii codes & store it in array like below mentioned example of JavaScript code.

for ( i  =  0;  i  <  inputString.length;  i++ ) {

      asciiArr[i]  =  inputString[i].charCodeAt( 0 ); 

 

Step - 2

Fill A to Z array in capital or small letters

for ( i = 0, code = 65; i < 26; i++, code++){

    atozArr[i] = String.fromCharCode(code);

}

 

Step - 3

Choose randomly two different characters index from A to Z and find which is minimum index and which is maximum index and stored it in different variables.

rndPositionOne = randomIndexFromInterval(0, atozArr.length - 1);

rndPositionTwo = randomIndexFromInterval(0, atozArr.length - 1);
 
minIndex = 0; maxIndex = 0;
 
if (rndPositionOne < rndPositionTwo){
   minIndex = rndPositionOne; maxIndex = rndPositionTwo;
}else{
   minIndex = rndPositionTwo; maxIndex = rndPositionOne;
}

Note: Here i'm referencing an function return a random value from minimum - maximum range.

Write a function outside of main function of your code. 

function randomIndexFromInterval( min, max){
   return Math.floor(Math.random() *   (max-min+1)+min);
}

 

Step - 4

Convert Randomly selected two indexes to Characters & Store it in selPosArray

for (i = minIndex, start = 0; i < maxIndex; i++, start++){    

 selPosArray[start] = String.fromCharCode( atozArr[i].charCodeAt(0));

}   

 

Step - 5

Addition of every selPosArray element to inputString element ascii

temp = selPosArray[selPosArray.length - 1].charCodeAt(0);

for (i = 0, j = selPosArray[0].charCodeAt(0); i < asciiArr.length; i++, j++){          

    if ( j == temp){
         j = selPosArray[0].charCodeAt(0);
    }
    encryptedString[i] = parseInt(asciiArr[i]) + j;         

}

 

Step - 6

Attach key to encrypted string - b'coz we use Symmetric Key encryption method

outputString.innerHTML = minIndex;

for (i = 0; i < encryptedString.length;  i++){   

      outputString.innerHTML = outputString.innerHTML +    String.fromCharCode(encryptedString[i]);

}

outputString.innerHTML = outputString.innerHTML + maxIndex; 

 

Step - 7

Finally your encryption is ready to send

document.getElementById("inputBox").value = outputString.innerHTML;

Plain Text:

Hi I am writing a blog on blogspot.com

Cypher Text:

4¯g‘i«¸l¼¸°¼²¸²l¦f©´¸±k»³f©´¸±¾¼´Âºu«¸·13

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

Decryption process is reverse of encryption process and here we use Symmetric Key concept so your decryption tool already knows where & how to decrypt the cipher text. 

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

 

 

 JavaScript Code for Practice

function encryption(){                
          var inputString = document.getElementById("input").value;
          var outputString = document.getElementById("enc");
          var asciiArr = new Array();
          var atozArr = new Array();
          var selPosArray = new Array();
          var indexedArray = new Array();
          var encryptedString = new Array();                
          if (inputString.length != 0){
             outputString.innerHTML = "";
             //First Step: Convert all characters in ascii code 
             for (i = 0; i < inputString.length; i++){
                asciiArr[i] = inputString[i].charCodeAt(0);
             }                    
             //Second Step: Fill AtoZ array in capital or small letters 
             for (i = 0, code = 65; i < 26; i++, code++){
                atozArr[i] = String.fromCharCode(code);                 }
             //Third Step: Choose random two character indexes from A to Z
             rndPositionOne = randomIndexFromInterval(0, atozArr.length - 1);
             rndPositionTwo = randomIndexFromInterval(0, atozArr.length - 1);
             minIndex = 0; maxIndex = 0;
             if (rndPositionOne < rndPositionTwo){
                 minIndex = rndPositionOne; maxIndex = rndPositionTwo;
             }else{
                 minIndex = rndPositionTwo; maxIndex = rndPositionOne;
             }                        
             //Fourth Step: Convert Randomly selected two indexes to Characters & Store it in selPosArray
             for (i = minIndex, start = 0; i < maxIndex; i++, start++){
             selPosArray[start] = String.fromCharCode(atozArr[i].charCodeAt(0));
             }
             //Fifth Step: Addition of every selPosArray element to inputString element ascii

             temp = selPosArray[selPosArray.length - 1].charCodeAt(0);
             for (i = 0, j = selPosArray[0].charCodeAt(0); i < asciiArr.length; i++, j++){
               if ( j == temp){
                    j = selPosArray[0].charCodeAt(0);
               }
               encryptedString[i] = parseInt(asciiArr[i]) + j;
        }
             //Sixth Step: Attach key to encrypted string
             outputString.innerHTML = minIndex;
             for (i = 0; i < encryptedString.length;  i++){
                outputString.innerHTML = outputString.innerHTML + String.fromCharCode(encryptedString[i]);
             }
             outputString.innerHTML = outputString.innerHTML + maxIndex;                        
             //Seventh Step: Finally your encryption is ready to send

            document.getElementById("inputBox").value = outputString.innerHTML;    
         }else{                        document.getElementById("enc").innerHTML = "Error: Value can not be empty.";
         return false;
        }

}

2 comments: