CaesarCipherDecryption

From GIS CS4
Jump to: navigation, search

We have written a program to encrypt a sample text using a basic Caesar cipher.

We are now going to implement a decryption algorithm.

The text is encrypted by adding a Caesar key value (0-25). The resulting text can be decrypted by subtracting that same key value. The same effect can be achieved by adding 26-key value. In other words, we can use our same Crypt() function to either encrypt or decrypt. We just need to know the key!

So how do we work out the key?

There are only 26 possible values of the key, and so one method would be trial and error.

We can use the computer to perform this task.

We know when we have found the key when the resulting text is plain English.

We can look at the text and read it, but how can the computer test for this?

We can use what could be called a Brute Force method. We provide a dictionary, or list, of most commonly used English words and write a program to count the number of occurrences of these common words in the decoded text.

The key value that results in the highest occurrence of common English words is most likely the correct one.

Exercise 1. Word Count?

  • Write a function to count the number of occurrences of a word in the text.
 Hint: Use the JavaScript function str.indexOf(searchvalue, start)
  • Make your function case INSENSITIVE!
 Hint: Use the JavaScript function str.toUpperCase()
  • Use an integer variable to keep count of occurrences (remember to initialise).
  • Use an integer variable to keep track of how far you have searched.
  • Use a boolean variable to decide when to stop searching (remember to initialise).
  • Use a while loop to search.
  • Test that your function works!
  • Save program!
/* return number of occurrences of zStr in zstrMsg */
var getWordCount = function(zstrMsg, zStr) 
{
    var iWordCount = 0;
    var index = 0;
    var bKeepSearching = true;

    // convert message text AND search string to upper case ... 

    // search here...

    return iWordCount;
};

...

// test word count function
var strMsg = "The quick brown fox jumped over the lazy dog with ease!";
var strSearch = "the";
var iWordCount = getWordCount(strMsg , strSearch);
text("Test String: " + strMsg , 50, 250);
text("SearchString: " + strSearch , 100, 250);
text("Word Count: " + iWordCount , 150, 250);

Exercise 2. Dictionary Look-up

  • Write a function to count the number of occurrences of commonly used words in the text.
  • Test that your function works.
  • Define a list of common words in an array.
  • Define an integer variable to keep count of the total occurrences of all common words.
  • Use a for loop to add in the number of occurrences of each word in the list.
  • Save program!
/* return number of occurences of common words in zstrMsg */
var getCommonWordCount = function(zstrMsg) 
{
    var strDictionary=["THE","AND","BUT","ALSO", "WAS", "OUR", 
                    "FOR", "WITH"];
    
    /* for each word in the dictionary */
    var iWordCount = 0;

    // for each word in the dictionary, add the number of occurrences ...

    return iWordCount;
};

...
// test common word count function
var iWordCount = getCommonWordCount ("The quick brown fox jumped over the lazy dog with ease!");
text("Frequency: " + iWordCount , 50, 200);

Exercise 3. Find the Key!

  • Write a function to loop through all possible key values (0-25) and find the key value that results in the greatest number of occurrences of common words.
  • If a best match is found then return the key value, else return -1.
/* find the best fit key using common word check
   return key value (-1 means unsuccessful).
*/
var findBestFitKey = function(zStr)
{
    var iBestFitKey = 0;
    var iBestWordCount = 0;
    
    // try all key values 0-25 and look for best match
    ...
    
    if (iBestWordCount > 0)
    {
        return iBestFitKey;
    }
    return -1;  // no match found
};
  • Insert some test data into your program.
  • Modify your program to find the best fit key and decrypt the test string.
  • If successful then output results (key value, word count, decrypted msg), otherwise output a message.
  • Save program!
var strEncryptedMsg = "ADRPITS XC RWXPCV BPX, IWPXAPCS, VGPRT\n" +
    " XCITGCPIXDCPA HRWDDA LPH RGTPITS ID\n" +
    " EGDKXST P WXVW FJPAXIN RWGXHIXPC\n" +
    " TSJRPIXDC UDG IWT RWXASGTC DU\n" +
    " XCITGCPIXDCPA LDGZTGH ADRPITS XC HDJIWTPHI\n" +
    " PHXP. DJG EGXBPGN VDPA PI VXH XH ID PAADL\n" +
    " IWTHT UPBXAXTH ID RDCIXCJT ID LDGZ XC IWTXG\n" +
    " UXTASH LXIWDJI WPKXCV ID RDBEGDBXHT IWT\n" +
    " TSJRPIXDC DU IWTXG RWXASGTC.";

/* display encrypted message in red */
textSize(14);
fill(255, 0, 0);    // red
text("Encrypted message:\n" + strEncryptedMsg, 20, 20);

var iKey;   

// call findBestFitKey function ...

// output results
fill(0, 0, 255);    // blue
if (iKey >= 0)
{
    var strDecryptedMsg = decrypt(strEncryptedMsg, iKey);
    var iWordCount = getCommonWordCount(strDecryptedMsg);

    text("Decrypted msg (key = " + iKey + 
            ", Word count = " + iWordCount + " ):\n" + 
            strDecryptedMsg, 20, 220);
}
else
{
    text("Failed to decrypt (no common words found in text)", 
            20, 220);
}
Personal tools
Namespaces

Variants
Actions
Navigation
Tools