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*****/
// string
// ctype.h
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static bool Cipher(string input, string oldAlphabet, string newAlphabet, string &output)
{
	output = "";
	int inputLen = input.size();

	if (oldAlphabet.size() != newAlphabet.size())
		return false;

	for (int i = 0; i < inputLen; ++i)
	{
		int oldCharIndex = oldAlphabet.find(tolower(input[i]));

		if (oldCharIndex >= 0)
			output += isupper(input[i]) ? toupper(newAlphabet[oldCharIndex]) : newAlphabet[oldCharIndex];
		else
			output += input[i];
	}

	return true;
}

static bool Encipher(string input, string cipherAlphabet, string &output)
{
	string plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
	return Cipher(input, plainAlphabet, cipherAlphabet, output);
}

static bool Decipher(string input, string cipherAlphabet, string &output)
{
	string plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
	return Cipher(input, cipherAlphabet, plainAlphabet, output);
}
								


Example

									string text = "The quick brown fox jumps over the lazy dog";
string cipherAlphabet = "yhkqgvxfoluapwmtzecjdbsnri";
string cipherText;
string plainText;

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"