Caesar Cipher, which is also called a substitution cipher,
replaces each letter of the alphabet with the three places after that original
alphabet. So for a Caesar Cipher the alphabet pattern would be
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
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
Example : Using Caesar Cipher “Rapid bits”
translates to “udslg elwv”. Here
Rapid bits is plain message (P) where as
“udslg elwv” is ciphered message (C)
So, if we generalize the idea of replacing each character
with a new letter after nth letter, we arrive to the following equation. This
can be easily tested by assigning a number to each letter as
Encrypted message is a function of position, so C=E(P)=f(P+n-1)%26 + 1
Above message can be Decrypted as P=D(C)=(C-n+25)%26 +1
Note in the above equations % is a modulus
Rapid bits original number coding without shifting
positions is
18 1 16 9 4 2 9 20
19
then if we shift 3 positions, 1st letter
“R”, with number 18, transforms to
C=E(18+3-1)%26 + 1 = 20+1=21
Here is a encrypted message with its number values
21 4 19 12 7 5 12 23
22.
So what is secret here? As you already noticed shifting a
letter to nth character is a secret. Since there are only 26 alphabets in
English, there are only 25 secret keys. Using brute force attack it is easy to
decrypt the messages. However ASCII
table numbering is different from the above 1 to 26 number identification
system.
Here is a script to encrypt a message by choosing a rotating
position. You may need to figure out how to decrypt for nth rotation by
reversing the flow for different positions.
Rot5, Rot13, Rot18, Rot47 are widely used, but they are not safe at all.
Rot is an acronym for rotation.
1)
RotN Encryptor
private string RotNEncrypt(string
s, int n, bool
includeNumbers)
{
string
result = "";
char[]
carray = s.ToCharArray();
for
(int i = 0; i < carray.Length; i++)
{
int
ascii = carray[i];
int
rot = ascii;
if
(ascii > 64 && ascii < 91)
{
rot = rot + n;
if
(rot > 90)
{
rot += -90 + 64;
}
if
(rot < 65)
{
rot += -64 + 90;
}
}
//
Lowercase letters are 97 to 122
else
if (ascii > 96 && ascii < 123)
{
rot = rot + n;
if
(rot > 122) rot += -122 + 96;
if
(rot < 97) rot += -96 + 122;
}
//
Numeric digits are 48 to 57
if
(includeNumbers == true && ascii >
47 && ascii < 58)
{
rot = rot + n;
if
(rot > 47) rot += -57 + 47;
if
(rot < 58) rot += -47 + 57;
}
char
character = (Char)rot;
result = result +
character.ToString();
}
return
result;
}
2)
Rot13 Encrypter and Decrypter
private string Rot13(string
s)
{
if
(String.IsNullOrEmpty(s)) return s;
char[]
character = s.ToCharArray();
for
(int i = 0; i < s.Length; i++)
{
char
c = s[i];
if
(c >= 97 && c <= 122)
{
int
rot = c + 13;
if
(rot > 122) rot -= 26;
character[i] = (char)rot;
}
else
if (c >= 65 && c <= 90)
{
int
rot = c + 13;
if
(rot > 90) rot -= 26;
character[i] = (char)rot;
}
else
{
character[i] = c;
}
}
return
new string(character);
}
3)
Rot47 Encryptor and Decryptor
private string Rot47(string
s)
{
string
cipher = "";
foreach
(char c in
s.ToCharArray())
{
if
(c != ' ')
{
int
rot = c;
rot += 47;
if
(rot > 126) rot -= 94;
if
(rot < 33) rot += 94;
cipher = cipher + (char)rot.ToString();
}
else
{
cipher = cipher + " ";
}
}
return
cipher;
}