Click here to Skip to main content
15,881,877 members
Articles / Programming Languages / C#

Simple String Encryption and Decryption with Source Code

Rate me:
Please Sign up or sign in to vote.
4.72/5 (50 votes)
21 Feb 2008CPOL 273.3K   11.2K   62   48
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:

Image 3

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:

C#
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)


Written By
Software Developer (Senior) Santhisoft Technologies
India India

Comments and Discussions

 
GeneralMy vote of 5 Pin
Bekalu Tenna17-Jan-23 6:33
Bekalu Tenna17-Jan-23 6:33 
GeneralMy vote of 5 Pin
Bekalu Tenna17-Jan-23 6:33
Bekalu Tenna17-Jan-23 6:33 
QuestionEven after a decade quick and easy. Pin
ssathy6219-Apr-19 7:52
ssathy6219-Apr-19 7:52 
GeneralMy vote of 5 Pin
OffCenter26-Feb-18 8:58
OffCenter26-Feb-18 8:58 
Questiondoubt Pin
Member 124346633-Apr-16 21:16
Member 124346633-Apr-16 21:16 
QuestionGood Code. Good Style Pin
Michael Breeden16-Nov-15 3:27
Michael Breeden16-Nov-15 3:27 
QuestionError for encrypting and decrypting MAC address Pin
auto981715-Sep-15 11:16
auto981715-Sep-15 11:16 
QuestionSetKey() ?????? Pin
84214413334-Jun-15 0:35
professional84214413334-Jun-15 0:35 
QuestionMessage Closed Pin
29-Jun-14 13:28
Bartosz Wójcik29-Jun-14 13:28 
GeneralMy vote of 4 Pin
Balasubramanian T29-Aug-13 20:10
Balasubramanian T29-Aug-13 20:10 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi29-Jul-13 0:36
professionalAmir Mohammad Nasrollahi29-Jul-13 0:36 
GeneralRe: My vote of 4 Pin
Lovely M29-Jul-13 19:46
Lovely M29-Jul-13 19:46 
GeneralMy vote of 5 Pin
vkx_13-Mar-13 5:05
vkx_13-Mar-13 5:05 
Questionhi Pin
Bartosz Wójcik12-Feb-13 10:39
Bartosz Wójcik12-Feb-13 10:39 
AnswerRe: hi Pin
Lovely M29-Jul-13 19:46
Lovely M29-Jul-13 19:46 
GeneralMy vote of 5 Pin
Michael Haephrati3-Dec-12 5:17
professionalMichael Haephrati3-Dec-12 5:17 
Thanks for a great article. Please also look at mine own article about Strings Obfuscation: http://www.codeproject.com/Articles/502283/Strings-Obfuscation-System
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 19:46
Lovely M29-Jul-13 19:46 
GeneralMy vote of 5 Pin
Vijay Raghava Reddy29-Aug-12 0:45
Vijay Raghava Reddy29-Aug-12 0:45 
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 19:47
Lovely M29-Jul-13 19:47 
GeneralRe: My vote of 5 Pin
Vijay Raghava Reddy9-Aug-13 1:32
Vijay Raghava Reddy9-Aug-13 1:32 
GeneralMy vote of 5 Pin
Bob Brown 1227-Mar-12 8:00
Bob Brown 1227-Mar-12 8:00 
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 19:47
Lovely M29-Jul-13 19:47 
GeneralMy vote of 3 Pin
jawaharraj8914-Mar-12 20:11
jawaharraj8914-Mar-12 20:11 
GeneralRe: My vote of 3 Pin
Lovely M29-Jul-13 19:48
Lovely M29-Jul-13 19:48 
QuestionNice article Pin
lintovadakkekudy6-Mar-12 20:11
lintovadakkekudy6-Mar-12 20:11 

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

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