Introduction
Many of you know what exactly Encryption is. It is the process of transforming information (referred to as plain text) using an Algorithm (called cipher) to make it unreadable to anyone except those possessing special knowledge, usually referred to as a Key. You might be thinking "so this is one more article in the long list of available Encryption Technology". But hey, this is different and let me explain how.
- It is simple and can be used with any version of .NET Framework (even you can easily convert and use it in Java and C++).
- It does not uses any Cryptography Library provided by .NET (like
System.Security.Cryptography).
- Less Memory consumption and Algorithm used is custom without using Cryptography Algorithm like MD5, SHA1, etc.
- It does not increases the
Length of string after Encryption (like in many other Algorithms) causing less Network usage between the two communicating parties.
Algorithm
In this, we have two inputs to the Cipher. One is the message that is needed to be Encrypted/Decrypted and the Key.
For Encryption: Break Message string and Key string into characters. Then for each character's ASCII value in Message string, add the corresponding character's ASCII value of Key string. If the Key string exhausts while adding its ASCII value to Message string, then restart adding from beginning character of Key.
Refer to the picture below for Message to Encrypt="MY NAME IS SANJAY" and Secret Key="Sa12#"
Now as you can see in the above picture, when ASCII value of Character 'M' is added with ASCII value 'S' of Encryption key, it generates an Encrypted character ' á '. Similarly it proceeds towards the end of Message string. Also please note in the above that when Encryption Key is exhausted while adding then we restart adding key from beginning (I have shown that using green and yellow color).
For Decryption: In this, we do just the opposite of the above. We take the Encrypted data and start subtracting the key characters from that which results in the original string.
Refer to the picture below:
Now as you can see in the above picture, ASCII value of key characters are subtracted from the Encrypted string resulting in the Original string.
Using the Code
Lots of theory till now. Let's jump on the code.
In the below provided code, you can see that method I have created only one function for both encrypting and decrypting the Message. The below shown method takes 3 parameters Message String, Key String and an enumerator enEnCryptDecrypt which acts as Flag for the method to decide for processing and proving an Encryption or Decryption Output.
public static string enCode(String Message, String Key, enEnCryptDecrypt EnC)
{
int messageCount, keyCount, value = 0;
StringBuilder strRetVal = new StringBuilder();
for (messageCount = 0, keyCount = 0; messageCount < Message.Length;
messageCount++, keyCount++)
{
if (keyCount >= Key.Length) keyCount = 0;
if (EnC == enEnCryptDecrypt.ENCRYPT)
value = (Message[messageCount]) + (Key[keyCount]);
else
value = (Message[messageCount]) - (Key[keyCount]);
strRetVal.Append((char)(value));
}
return strRetVal.ToString(); }
In the above provided code, the KeyCount variable rotates the Key to the beginning if it is exhausted. Also enCryptDeCrypt Enumerator decides for adding or subtracting key character's ASCII value from Message character's ASCII value. Finally value variable which is an int is type caste to character value and then append to StringBuffer class.
Below is the definition of Enumerator used in the above method enCode.
public enum enEnCryptDecrypt
{
DECRYPT, ENCRYPT
};
How To Use
I have provided a GUI for using the EnCryption/DeCryption. You can use the Class file containing code for Encryption directly into your project or if you want, you can separately compile it as DLL and add that as reference to your project.
Conclusion
This code will run with any version of .NET and can be very easily converted to other languages as well.
History
- 12th February 2011: Initial post