Click here to Skip to main content
Licence CPOL
First Posted 20 Feb 2008
Views 71,606
Downloads 2,141
Bookmarked 38 times

Simple String Encryption and Decryption with Source Code

By | 21 Feb 2008 | Article
Sample program for simple encryption and decryption of strings

Introduction

This article helps the C# beginners to do simple encryption and decryption of strings. It will be useful for simple password encryption or any such string encryption.

Encryption using the default key:

Encryption1.jpg

Encryption using the user's custom key:

Encryption.jpg

Decryption using the user's custom key:

Using the Code

The demo application uses a class SSTCryptographer which contains two static overloaded methods and a static property. 

How To Use the Given Code

On Button clicks:

private void btnEncrypt_Click(object sender, EventArgs e)
{ 
txtOutputText.Text = SSTCryptographer.Encrypt(txtInputText.Text,SetKey());
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
txtOutputText.Text = SSTCryptographer.Decrypt(txtInputText.Text,SetKey());
}

/// <summary>
/// Encrypt the given string using the default key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt)
{
    try
    {
        return Encrypt(strToEncrypt, _key);
    }

    catch (Exception ex)
    {
        return "Wrong Input. " + ex.Message;
    }
} 

/// <summary>
/// Decrypt the given string using the default key.
/// </summary>
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted)
{
    try
    {
        return Decrypt(strEncrypted, _key);
    }
    catch (Exception ex)
    {
        return "Wrong Input. " + ex.Message;
    }
}

/// <summary>
/// Encrypt the given string using the specified key.
/// </summary>
/// <param name="strToEncrypt">The string to be encrypted.</param>
/// <param name="strKey">The encryption key.</param>
/// <returns>The encrypted string.</returns>
public static string Encrypt(string strToEncrypt, string strKey)
{
    try
    {
        TripleDESCryptoServiceProvider objDESCrypto = 
			new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
        byte[] byteHash, byteBuff;
        string strTempKey = strKey;
        byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
        objHashMD5 = null;
        objDESCrypto.Key = byteHash;
        objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
        byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
        return Convert.ToBase64String(objDESCrypto.CreateEncryptor().
			TransformFinalBlock(byteBuff, 0, byteBuff.Length));
    }
    catch (Exception ex)
    {
        return "Wrong Input. " + ex.Message;
    }
}

/// <summary>
/// Decrypt the given string using the specified key.
/// </summary>
/// <param name="strEncrypted">The string to be decrypted.</param>
/// <param name="strKey">The decryption key.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string strEncrypted, string strKey)
{
    try
    {
        TripleDESCryptoServiceProvider objDESCrypto = 
			new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
        byte[] byteHash, byteBuff;
        string strTempKey = strKey;
        byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
        objHashMD5 = null;
        objDESCrypto.Key = byteHash;
        objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
        byteBuff = Convert.FromBase64String(strEncrypted);
        string strDecrypted = ASCIIEncoding.ASCII.GetString
		(objDESCrypto.CreateDecryptor().TransformFinalBlock
		(byteBuff, 0, byteBuff.Length));
        objDESCrypto = null;
        return strDecrypted;
    }
    catch (Exception ex)
    {
        return "Wrong Input. " + ex.Message;
    }
}

License

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

About the Author

Lovely M

Software Developer
Santhisoft Technologies
India India

Member

Lovely Manuel
Santhisoft Technologies
 
Website Template, Simple POS Software, Personal Accounting Software, Video Rental Software, Time Table Software
Tutorials
VC++ Tutorials, C#.Net Tutorials, VB.Net Tutorials

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 PinmemberBob Brown 128:00 27 Mar '12  
GeneralMy vote of 3 Pinmemberjawaharraj8920:11 14 Mar '12  
QuestionNice article Pinmemberlintovadakkekudy20:11 6 Mar '12  
QuestionCharacter sets PinmemberMark Roebuck2:32 3 Sep '11  
AnswerRe: Character sets Pinmemberglennswest23:48 6 Oct '11  
GeneralI want to use my code for encrypt PinmemberMayur Gujrathiii22:00 26 May '11  
QuestionENCRYPT AND DECRYPT HELP Pinmemberrsdelrosario22:58 28 Jul '10  
QuestionRe: ENCRYPT AND DECRYPT HELP Pinmemberrsdelrosario23:04 28 Jul '10  
GeneralHELPPPPPPPPPPPPPP Pinmemberbarbiez8:14 23 May '10  
GeneralRe: HELPPPPPPPPPPPPPP PinmemberLovely M18:14 23 May '10  
GeneralThanks PinmemberMushtaque Nizamani20:20 7 May '09  
GeneralRe: Thanks PinmemberLovely M21:48 7 May '09  
QuestionMessage Removed Pinmembermarzieh minooyee20:41 19 Dec '08  
AnswerRe: qusetuion PinmemberLovely M17:53 21 Dec '08  
GeneralString reference not set to an instance of String PinmemberAsanda1:48 8 Sep '08  
GeneralRe: String reference not set to an instance of String PinmemberLovely M18:37 8 Sep '08  
GeneralRe: String reference not set to an instance of String PinmemberAsanda2:46 9 Sep '08  
Thanks. Now it is giving me this error(Bad Data).I'm using the default key.
 
A Koom

GeneralRe: String reference not set to an instance of String PinmemberLovely M18:35 9 Sep '08  
GeneralThanks! Thank You! And More Thanks! Pinmembermicrobus11:13 22 Jul '08  
GeneralThanks a lot PinmemberAbhi suryawanshi21:35 4 Jul '08  
QuestionInvalid length for a Base-64 char array. Pinmembermoistllama4:30 20 Mar '08  
AnswerRe: Invalid length for a Base-64 char array. PinmemberAmar Chaudhary20:34 29 Apr '08  
QuestionHow to decrypt an encrypted string using MD5 algorithm PinmemberMember 277185117:58 22 Feb '08  
GeneralNice article PinmemberRajib Ahmed9:13 20 Feb '08  
GeneralRe: Nice article [modified] PinmemberLovely M9:55 20 Feb '08  

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
Web02 | 2.5.120604.1 | Last Updated 21 Feb 2008
Article Copyright 2008 by Lovely M
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid