Click here to Skip to main content
12,623,903 members (30,751 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

178.2K views
8.9K downloads
59 bookmarked
Posted

Simple String Encryption and Decryption with Source Code

, 21 Feb 2008 CPOL
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questiondoubt Pin
Member 124346633-Apr-16 22:16
memberMember 124346633-Apr-16 22:16 
QuestionGood Code. Good Style Pin
Michael Breeden16-Nov-15 4:27
memberMichael Breeden16-Nov-15 4:27 
QuestionError for encrypting and decrypting MAC address Pin
auto981715-Sep-15 12:16
memberauto981715-Sep-15 12:16 
QuestionSetKey() ?????? Pin
77981713014-Jun-15 1:35
professional77981713014-Jun-15 1:35 
QuestionString encryption tool (encryptor and string decryptor code generator) Pin
Bartosz Wojcik29-Jun-14 14:28
memberBartosz Wojcik29-Jun-14 14:28 
GeneralMy vote of 4 Pin
Balasubramanian T29-Aug-13 21:10
memberBalasubramanian T29-Aug-13 21:10 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi29-Jul-13 1:36
professionalAmir Mohammad Nasrollahi29-Jul-13 1:36 
GeneralRe: My vote of 4 Pin
Lovely M29-Jul-13 20:46
memberLovely M29-Jul-13 20:46 
GeneralMy vote of 5 Pin
vkxvkx13-Mar-13 6:05
membervkxvkx13-Mar-13 6:05 
Questionhi Pin
Bartosz Wojcik12-Feb-13 11:39
memberBartosz Wojcik12-Feb-13 11:39 
AnswerRe: hi Pin
Lovely M29-Jul-13 20:46
memberLovely M29-Jul-13 20:46 
GeneralMy vote of 5 Pin
Michael Haephrati מיכאל האפרתי3-Dec-12 6:17
memberMichael Haephrati מיכאל האפרתי3-Dec-12 6:17 
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 20:46
memberLovely M29-Jul-13 20:46 
GeneralMy vote of 5 Pin
Vijay Raghava Reddy29-Aug-12 1:45
memberVijay Raghava Reddy29-Aug-12 1:45 
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 20:47
memberLovely M29-Jul-13 20:47 
GeneralRe: My vote of 5 Pin
Vijay Raghava Reddy9-Aug-13 2:32
memberVijay Raghava Reddy9-Aug-13 2:32 
GeneralMy vote of 5 Pin
Bob Brown 1227-Mar-12 9:00
memberBob Brown 1227-Mar-12 9:00 
GeneralRe: My vote of 5 Pin
Lovely M29-Jul-13 20:47
memberLovely M29-Jul-13 20:47 
GeneralMy vote of 3 Pin
jawaharraj8914-Mar-12 21:11
memberjawaharraj8914-Mar-12 21:11 
GeneralRe: My vote of 3 Pin
Lovely M29-Jul-13 20:48
memberLovely M29-Jul-13 20:48 
QuestionNice article Pin
lintovadakkekudy6-Mar-12 21:11
memberlintovadakkekudy6-Mar-12 21:11 
AnswerRe: Nice article Pin
Lovely M29-Jul-13 20:48
memberLovely M29-Jul-13 20:48 
QuestionCharacter sets Pin
Mark Roebuck3-Sep-11 3:32
memberMark Roebuck3-Sep-11 3:32 
AnswerRe: Character sets Pin
glennswest7-Oct-11 0:48
memberglennswest7-Oct-11 0:48 
GeneralI want to use my code for encrypt Pin
Mayur Gujrathiii26-May-11 23:00
memberMayur Gujrathiii26-May-11 23:00 
QuestionENCRYPT AND DECRYPT HELP Pin
rsdelrosario28-Jul-10 23:58
memberrsdelrosario28-Jul-10 23:58 
QuestionRe: ENCRYPT AND DECRYPT HELP Pin
rsdelrosario29-Jul-10 0:04
memberrsdelrosario29-Jul-10 0:04 
GeneralHELPPPPPPPPPPPPPP Pin
barbiez23-May-10 9:14
memberbarbiez23-May-10 9:14 
GeneralRe: HELPPPPPPPPPPPPPP Pin
Lovely M23-May-10 19:14
memberLovely M23-May-10 19:14 
GeneralThanks Pin
Mushtaque Nizamani7-May-09 21:20
memberMushtaque Nizamani7-May-09 21:20 
GeneralRe: Thanks Pin
Lovely M7-May-09 22:48
memberLovely M7-May-09 22:48 
AnswerRe: qusetuion Pin
Lovely M21-Dec-08 18:53
memberLovely M21-Dec-08 18:53 
GeneralString reference not set to an instance of String Pin
Asanda8-Sep-08 2:48
memberAsanda8-Sep-08 2:48 
GeneralRe: String reference not set to an instance of String Pin
Lovely M8-Sep-08 19:37
memberLovely M8-Sep-08 19:37 
GeneralRe: String reference not set to an instance of String Pin
Asanda9-Sep-08 3:46
memberAsanda9-Sep-08 3:46 
GeneralRe: String reference not set to an instance of String Pin
Lovely M9-Sep-08 19:35
memberLovely M9-Sep-08 19:35 
GeneralThanks! Thank You! And More Thanks! Pin
microbus22-Jul-08 12:13
membermicrobus22-Jul-08 12:13 
GeneralThanks a lot Pin
Abhi suryawanshi4-Jul-08 22:35
memberAbhi suryawanshi4-Jul-08 22:35 
QuestionInvalid length for a Base-64 char array. Pin
moistllama20-Mar-08 5:30
membermoistllama20-Mar-08 5:30 
AnswerRe: Invalid length for a Base-64 char array. Pin
Amar Chaudhary29-Apr-08 21:34
memberAmar Chaudhary29-Apr-08 21:34 
QuestionHow to decrypt an encrypted string using MD5 algorithm Pin
Member 277185122-Feb-08 18:58
memberMember 277185122-Feb-08 18:58 
GeneralNice article Pin
Rajib Ahmed20-Feb-08 10:13
memberRajib Ahmed20-Feb-08 10:13 
GeneralRe: Nice article [modified] Pin
Lovely M20-Feb-08 10:55
memberLovely M20-Feb-08 10:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 21 Feb 2008
Article Copyright 2008 by Lovely M
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid