Saturday, March 24, 2012

Simple Substitution Cipher Algorithms in C

A substitution cipher is a method of encryption by which units of the original alphabet (or plain text) are replaced with units of a coded alphabet (or cipher text) according to a regular system. The units may be single letters, two letters or triplets or letters, etc. A simple substitution cipher uses operates with single letter units.

To encrypt a message using a substitution cipher one needs the plain text alphabet (original alphabet) and the cipher text alphabet (the coded alphabet) which shall serve as a private key.
The formula for the substitution is:
  • Ei represents the i-th character from the encoded string
  • Cj represents the j-th character from the cipher text alphabet, where j represents the position of the Ei character in the original alphabet.
In order to implement such algorithm we shall define the following enumeration:
typedef enum
{
 OPERATION_SUCCESS = 1U,
 OPERATION_FAILED  = 0U
}OPERATION_STATUS;
Below you can see the C implementation for the algorithm:
OPERATION_STATUS SimpleSubstitutionCipher_Code(char* originalAlphabet,
                                               char* codedAlphabet,
                                               char* originalMessage,
                                               char* encodedMessage)
{
 unsigned short alphabetLength = strlen(originalAlphabet);
 unsigned int messageLength    = strlen(originalMessage);
 unsigned int i = 0U;
 char* pointer = NULL;
 unsigned int position = 0U;
 if( alphabetLength != strlen(codedAlphabet) )
 {
  //The lengths of the alphabets do not match
  return OPERATION_FAILED;
 }
 for(i=0; i<messageLength; i++)
 {
  pointer  = strchr(originalAlphabet, originalMessage[i]);
  if(pointer==NULL)
  {
   //A character in the message was not found in the
   //original alphabet
   return OPERATION_FAILED;
  }
  else
  {
   position = pointer - originalAlphabet;
   encodedMessage[i] = codedAlphabet[position];
  }
 }
 encodedMessage[messageLength] = '\0';
 return OPERATION_SUCCESS;
}
The function receives 4 parameters:
  • originalAlphabet - a pointer to a string containing the plain text alphabet (who has all the letters that appear in the originalMessage). All symbols in the originalAlphabet should appear only once.
  • codedAlphabet - a pointer to a string containing the cipher text alphabet (who has the same length as the originalAlphabet string). All symbols in the codedAlphabet should appear only once.
  • originalMessage - a pointer to a string containing the message who will be encoded
  • codedMessage - a pointer to a pre-allocated empty string who will contain the encoded message.
The function returns OPERATIONS_SUCCES if all preconditions have been respected, respectively OPERATON_FAILED if the lengths of the alphabets are different or if the function found a symbol in the original message who has not been defined in the originalAlphabet.

A simple encoding/decoding example
 char originalAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
 char codedAlphabet[]    = "QWERTYUIOPASDFGHJKLZXCVBNM#";
 char originalMessage[]  = "SUBSTITUTION CIPHER";
 char encodedMessage[20];
 char decodedMessage[20];
 //Encoding
 SimpleSubstitutionCipher_Code(originalAlphabet,codedAlphabet,
                               originalMessage, encodedMessage);
 puts(encodedMessage);
 //Decoding
 SimpleSubstitutionCipher_Code(codedAlphabet,originalAlphabet,
                               encodedMessage, decodedMessage);
 puts(decodedMessage);
As you probably already observed, the same function is used for both encryption and decryption. This is because the same operations are performed in both cases.
Related Posts Plugin for WordPress, Blogger...