Click here to Skip to main content
Licence CPOL
First Posted 11 Feb 2011
Views 12,972
Downloads 841
Bookmarked 9 times

Simplest Algo to Encrypt and Decrypt Data using Secret Key with C#

By | 12 Feb 2011 | Article
Implementation of simplest way to Encrypt and Decrypt Data without using any MD5 or Other Algorithm
Screen_Short.JPG

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.

  1. 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++).
  2. It does not uses any Cryptography Library provided by .NET (like System.Security.Cryptography).
  3. Less Memory consumption and Algorithm used is custom without using Cryptography Algorithm like MD5, SHA1, etc.
  4. 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#"

Encypt_Algo.JPG

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:

Decrypt_Algo.JPG

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; 
        // Rotates the Key string counter to Zero when character value 
        // is traversed till end

        if (EnC == enEnCryptDecrypt.ENCRYPT)
            value = (Message[messageCount]) + (Key[keyCount]); 
            //When Encryption is required we add ASCII value of key with Message
        else
            value = (Message[messageCount]) - (Key[keyCount]); 
            //When Decryption is required we subtract ASCII value of key with Message

        strRetVal.Append((char)(value)); 
        // Here we convert the ASCII value to character and Append that to Stringbuilder
    }           
           
    return strRetVal.ToString(); //Returns the Processed String.
} 

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.

/// <summary>
/// This Enumerator is used as a flag in EnCrypt method for deciding 
/// enCryption or deCryption.
/// </summary>
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sanjay K. Sinha

Software Developer (Senior)

India India

Member

Hi, I am sanjay from Pune,India. Currently working as Senior Software engineer(R&D)and always likes to hunt down interesting technical problems. Like to code in C# and java.
 
I have a wide range of skills and interests, including cryptography, image processing, computational linguistics, military history, 3D graphics, database optimization, and mathematics, to name a few. I generally do little besides work, also i have interest in reading novels and traveling via train.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:51 29 Mar '12  
GeneralMy vote of 5 PinmemberMmohmmad3:24 25 Mar '12  
GeneralEncrypt for 32 to 128 - Printable Charcters only PinmemberMGRB4:12 4 Oct '11  
GeneralRe: Encrypt for 32 to 128 - Printable Charcters only PinmemberSanjay K. Sinha18:57 12 Dec '11  
Generalthanks for sharing PinmemberPranay Rana3:36 25 Feb '11  
GeneralMy vote of 2 PinmemberBud Minton9:20 12 Feb '11  
GeneralMy vote of 2 PinmemberManfred R. Bihy7:30 12 Feb '11  
GeneralRe: My vote of 2 PinmemberSanjay K. Sinha7:35 12 Feb '11  
GeneralRe: My vote of 2 PinmemberManfred R. Bihy7:42 12 Feb '11  
GeneralRe: My vote of 2 PinmemberSanjay K. Sinha7:52 12 Feb '11  
GeneralBut what's the point? Pinmemberveki-peki3:36 13 Feb '11  
GeneralRe: But what's the point? PinmemberSanjay K. Sinha9:18 13 Feb '11  
GeneralRe: But what's the point? Pinmemberveki-peki9:38 13 Feb '11  
GeneralRe: But what's the point? PinmemberSanjay K. Sinha11:13 13 Feb '11  
GeneralRe: My vote of 2 Pinmemberveki-peki7:36 13 Feb '11  
GeneralRe: My vote of 2 PinmemberManfred R. Bihy7:44 13 Feb '11  
Generalit's called the keyword cipher PinmemberThomas Gerber1:56 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberSanjay K. Sinha5:11 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberThomas Gerber5:44 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberSanjay K. Sinha5:51 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberThomas Gerber6:25 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberSanjay K. Sinha6:45 12 Feb '11  
GeneralRe: it's called the keyword cipher Pinmemberveki-peki6:54 12 Feb '11  
GeneralRe: it's called the keyword cipher PinmemberSanjay K. Sinha7:41 12 Feb '11  
GeneralRe: it's called the keyword cipher Pinmemberveki-peki7:28 13 Feb '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Feb 2011
Article Copyright 2011 by Sanjay K. Sinha
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid