Simple Substitution Cipher
In cryptography, a simple substitution cipher is a cipher that has been in use for many hundreds of years. It is an improvement to the Caesar Cipher. Instead of shifting the alphabets by some number, this scheme substitutes every plaintext character for a different ciphertext character.
With 26 letters in alphabet, the possible permutations are 26! (Factorial of 26) which is equal to 4x1026. The sender and the receiver may choose any one of these possible permutation as a ciphertext alphabet. This permutation is the secret key of the scheme.
Example:
Here is a quick example for the encryption and decryption of simple substitution cipher. The text we will encrypt is 'cryptography'.
Keys for the simple substitution cipher usually consist of 26 letters. An example key is:
Plain Alphabet: | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Cipher Alphabet: | y | h | k | q | g | v | x | f | o | l | u | a | p | w | m | t | z | e | c | j | d | b | s | n | r | i |
An example encryption using the above key:
Plain Alphabet: | c | r | y | p | t | o | g | r | a | p | h | y |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Cipher Alphabet: | k | e | r | t | j | m | x | e | y | t | f | r |
It is easy to see how each character in the plaintext is replaced with the corresponding letter in the cipher alphabet. Decryption is just as easy, by going from the cipher alphabet back to the plain alphabet.
Security:
Simple substitution cipher is a considerable improvement over the Caesar Cipher. The possible number of keys is large (26!) and even the modern computing systems are not yet powerful enough to comfortably launch a brute force attack to break the system. However, the simple substitution cipher has a simple design and it is prone to design flaws, say choosing obvious permutation, this cryptosystem can be easily broken.
/*****Please include following header files*****/
// stdlib.h
// string.h
// ctype.h
// stdbool.h
/***********************************************/
bool Cipher(char* input, char* oldAlphabet, char* newAlphabet, char* output)
{
int inputLen = strlen(input);
if (strlen(oldAlphabet) != strlen(newAlphabet))
return false;
for (int i = 0; i < inputLen; ++i)
{
const char* ptr = strchr(oldAlphabet, tolower(input[i]));
int oldCharIndex = ptr - oldAlphabet;
if (ptr && oldCharIndex >= 0)
output[i] = isupper(input[i]) ? toupper(newAlphabet[oldCharIndex]) : newAlphabet[oldCharIndex];
else
output[i] = input[i];
}
output[inputLen] = '\0';
return true;
}
bool Encipher(char* input, char* cipherAlphabet, char* output)
{
char* plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
return Cipher(input, plainAlphabet, cipherAlphabet, output);
}
bool Decipher(char* input, char* cipherAlphabet, char* output)
{
char* plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
return Cipher(input, cipherAlphabet, plainAlphabet, output);
}
Example
char* text = "The quick brown fox jumps over the lazy dog";
char* cipherAlphabet = "yhkqgvxfoluapwmtzecjdbsnri";
char* cipherText = (char*)malloc(strlen(text));
char* plainText = (char*)malloc(strlen(cipherText));
bool encipherResult = Encipher(text, cipherAlphabet, cipherText);
bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText);
Output
cipherText: "Jfg zdoku hemsw vmn ldptc mbge jfg ayir qmx"
plainText: "The quick brown fox jumps over the lazy dog"