# 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: Cipher 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 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: Cipher Alphabet: c r y p t o g r a p h y 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.

									private static bool Cipher(string input, string oldAlphabet, string newAlphabet, out string output)
{
output = string.Empty;

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

for (int i = 0; i < input.Length; ++i)
{
int oldCharIndex = oldAlphabet.IndexOf(char.ToLower(input[i]));

if (oldCharIndex >= 0)
output += char.IsUpper(input[i]) ? char.ToUpper(newAlphabet[oldCharIndex]) : newAlphabet[oldCharIndex];
else
output += input[i];
}

return true;
}

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

public static bool Decipher(string input, string cipherAlphabet, out string output)
{
string plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
return Cipher(input, cipherAlphabet, plainAlphabet, out 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, out cipherText);
bool decipherResult = Decipher(cipherText, cipherAlphabet, out plainText);


### Output

									cipherText:	"Jfg zdoku hemsw vmn ldptc mbge jfg ayir qmx"
plainText:	"The quick brown fox jumps over the lazy dog"