How To Ideas | How To Articles | How To Tutorials


0

How To Implement Monoalphabetic Cipher using C


Monoalphabetic Cipher is a method to encrypt messages by substituting one character for another character. This is fairly a simple method for encrypting data. Alike Ceaser Cipher, which have only 26 possible keys, Monoalphabetic Cipher has 26! possible keys (or possible ways to encrypt messages). In this tutorial, I will use the key pair given on Wikipediaas an example to illustrate the working of this cipher. Also I will encrypt the same message given on the same page on Wikipedia.

Introductions:
  1. Key pair for this cipher is declared globally using the following statement.
    char key[2][26] = {"ZEBRASCDFGHIJKLMNOPQTUVWXY", "zebrascdfghijklmnopqtuvwxy"};
  • Now loop through the input text and check every character in the string. If the character is uppercase, the replace with the corresponding character from key[0] string. e.g if character is ‘A’, replace it with ‘Z’, if it is ‘D’, replace it with ‘R’. Do the same for lowercase characters and replace them with corresponding characters from key[1] string. But if character is any symbol, there is no need to encrypt it and leave it.
    for(temp = 0; temp < strlen(input); temp++)
    {
        if(isupper(input[temp]))
            output[temp] = key[0][(int)input[temp] - (int)'A'];
        else if(islower(input[temp]))
            output[temp] = key[1][(int)input[temp] - (int)'a'];
        else
            output[temp] = input[temp];
    }
  • To decrypt a message, find out the index of particular character in the string. e.g say character to be decoded is uppercase, then check the index of this character in key[0] string. now add up this index in 65 (ASCII value of character ‘A’) and typecast the result into a character.
    for(temp = 0; temp < strlen(input); temp++)
    {
        if(isupper(input[temp]))
            output[temp] = (char)((int)'A' + (strchr(key[0], input[temp]) - key[0]));
        else if(islower(input[temp]))
            output[temp] = (char)((int)'a' + (strchr(key[1], input[temp]) - key[1]));
        else
            output[temp] = input[temp];
    }
Encrypting Text:
Encrypt Text with Monoalphabetic Cipher
Decrypting Text:
Decrypt Text with Monoalphabetic Cipher
You can download the sample code from here.

Incoming search terms:

  • monoalphabetic cipher code in c (46)
  • monoalphabetic cipher program in c (10)
  • c program for mono alphabetic substitution (1)
Filed in: C, Security Tags: , , , , , ,

Leave a Reply

Submit Comment



© 9852 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.