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.



									Private Shared Function Cipher(input As String, oldAlphabet As String, newAlphabet As String, ByRef output As String) As Boolean
	output = String.Empty

	If oldAlphabet.Length <> newAlphabet.Length Then
		Return False
	End If

	For i As Integer = 0 To input.Length - 1
		Dim oldCharIndex As Integer = oldAlphabet.IndexOf(Char.ToLower(input(i)))

		If oldCharIndex >= 0 Then
			output += If(Char.IsUpper(input(i)), Char.ToUpper(newAlphabet(oldCharIndex)), newAlphabet(oldCharIndex))
		Else
			output += input(i)
		End If
	Next

	Return True
End Function

Public Shared Function Encipher(input As String, cipherAlphabet As String, ByRef output As String) As Boolean
	Dim plainAlphabet As String = "abcdefghijklmnopqrstuvwxyz"
	Return Cipher(input, plainAlphabet, cipherAlphabet, output)
End Function

Public Shared Function Decipher(input As String, cipherAlphabet As String, ByRef output As String) As Boolean
	Dim plainAlphabet As String = "abcdefghijklmnopqrstuvwxyz"
	Return Cipher(input, cipherAlphabet, plainAlphabet, output)
End Function
								


Example

									Dim text As String = "The quick brown fox jumps over the lazy dog"
Dim cipherAlphabet As String = "yhkqgvxfoluapwmtzecjdbsnri"
Dim cipherText As String
Dim plainText As String

Dim encipherResult As Boolean = Encipher(text, cipherAlphabet, cipherText)
Dim decipherResult As Boolean = 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"