Click here to Skip to main content
Click here to Skip to main content

Tagged as

Simple String Encryption and Decryption with Source Code

, 21 Feb 2008
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Lovely M
Software Developer (Senior) Santhisoft Technologies
India India

Comments and Discussions

 
QuestionString encryption tool (encryptor and string decryptor code generator) PinmemberBartosz Wojcik29-Jun-14 13:28 
GeneralMy vote of 4 PinmemberBalasubramanian T29-Aug-13 20:10 
GeneralMy vote of 4 PinprofessionalAmir Mohammad Nasrollahi29-Jul-13 0:36 
GeneralRe: My vote of 4 PinmemberLovely M29-Jul-13 19:46 
GeneralMy vote of 5 Pinmembervkxvkx13-Mar-13 5:05 
Questionhi PinmemberBartosz Wojcik12-Feb-13 10:39 
try http://www.stringencrypt.com instead, faster and easier + generates the decryptor code

AnswerRe: hi PinmemberLovely M29-Jul-13 19:46 
GeneralMy vote of 5 PinmemberMichael Haephrati מיכאל האפרתי3-Dec-12 5:17 
GeneralRe: My vote of 5 PinmemberLovely M29-Jul-13 19:46 
GeneralMy vote of 5 PinmemberVijay Raghava Reddy29-Aug-12 0:45 
GeneralRe: My vote of 5 PinmemberLovely M29-Jul-13 19:47 
GeneralRe: My vote of 5 PinmemberVijay Raghava Reddy9-Aug-13 1:32 
GeneralMy vote of 5 PinmemberBob Brown 1227-Mar-12 8:00 
GeneralRe: My vote of 5 PinmemberLovely M29-Jul-13 19:47 
GeneralMy vote of 3 Pinmemberjawaharraj8914-Mar-12 20:11 
GeneralRe: My vote of 3 PinmemberLovely M29-Jul-13 19:48 
QuestionNice article Pinmemberlintovadakkekudy6-Mar-12 20:11 
AnswerRe: Nice article PinmemberLovely M29-Jul-13 19:48 
QuestionCharacter sets PinmemberMark Roebuck3-Sep-11 2:32 
AnswerRe: Character sets Pinmemberglennswest6-Oct-11 23:48 
GeneralI want to use my code for encrypt PinmemberMayur Gujrathiii26-May-11 22:00 
QuestionENCRYPT AND DECRYPT HELP Pinmemberrsdelrosario28-Jul-10 22:58 
QuestionRe: ENCRYPT AND DECRYPT HELP Pinmemberrsdelrosario28-Jul-10 23:04 
GeneralHELPPPPPPPPPPPPPP Pinmemberbarbiez23-May-10 8:14 
GeneralRe: HELPPPPPPPPPPPPPP PinmemberLovely M23-May-10 18:14 
GeneralThanks PinmemberMushtaque Nizamani7-May-09 20:20 
GeneralRe: Thanks PinmemberLovely M7-May-09 21:48 
AnswerRe: qusetuion PinmemberLovely M21-Dec-08 17:53 
GeneralString reference not set to an instance of String PinmemberAsanda8-Sep-08 1:48 
GeneralRe: String reference not set to an instance of String PinmemberLovely M8-Sep-08 18:37 
GeneralRe: String reference not set to an instance of String PinmemberAsanda9-Sep-08 2:46 
GeneralRe: String reference not set to an instance of String PinmemberLovely M9-Sep-08 18:35 
GeneralThanks! Thank You! And More Thanks! Pinmembermicrobus22-Jul-08 11:13 
GeneralThanks a lot PinmemberAbhi suryawanshi4-Jul-08 21:35 
QuestionInvalid length for a Base-64 char array. Pinmembermoistllama20-Mar-08 4:30 
AnswerRe: Invalid length for a Base-64 char array. PinmemberAmar Chaudhary29-Apr-08 20:34 
QuestionHow to decrypt an encrypted string using MD5 algorithm PinmemberMember 277185122-Feb-08 17:58 
GeneralNice article PinmemberRajib Ahmed20-Feb-08 9:13 
GeneralRe: Nice article [modified] PinmemberLovely M20-Feb-08 9:55 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 21 Feb 2008
Article Copyright 2008 by Lovely M
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid