Click here to Skip to main content
13,901,026 members
Click here to Skip to main content
Add your own
alternative version


94 bookmarked
Posted 28 Dec 2006
Licenced CPOL

Encrypt and Decrypt a String

, 29 Dec 2008
Rate this:
Please Sign up or sign in to vote.
In this article, you can encrypt a string and decrypt it again
Sample Image - Encrypt_an_string1.gif


If you write a secure program, you should secure something such as a password, private key and something else…
For this propose, this article can help you to encrypt a string and decrypt a string by using some method.


This article explains 4 method to encrypt a string in .NET 2.0. There are 4 popular methods to encrypt a string:

  1. DES
  2. RC2
  3. Rijindeal
  4. Triple DES

Note: For all of these methods, we have some required parameters:

  • Specify a method
  • Use a Private Key

What is a Private Key?

Private Key is a string with specific Length.
For DES method, the length of Private key string must be 8 characters.
For RC2 method, the length of Private key string must be Minimum 7 characters.

What is Initialization Vector?

The purpose of the Initialization vector is to prevent dictionary attacks. For example, without an initialization vector, "password" will always encrypt to the same thing, which makes a dictionary attack easier. If something unique for each entry (such as row ID) is used as an initialization vector, an attacker would have to apply a dictionary attack to each row individually, as opposed to the entire DB. ( Thanks for Wk633...)

Using the Code

For implementing encrypt a string, we have 2 classes:

  • EncryptEngine
  • Encryptor

Class diagram of program

// EncryptEngine: // Core of Encryption class 
Public class EncryptEngine 
    EncrypltionAlgortim AlgorithmID; 
    Vector Keyword 
    Public EncryptEngine()
        // Constructor … Initializing Parameters 
    Public GetCryptTransformer() 
        // Return ICryptTransformer by Specified algoritm 
Public class Encryptor 
    EncryptEngine _engine; 
    String _privateKey=""; 
    Byte [] IV; 
    // Initialization Vector ==> Needs for decrypt 
    Public Encryptor(ALgoritmID,PrivateKey) 
        // <CODE>Initializing Encrypt Engine by setting Method and PrivateKey 
     Public string Encrypt(string SourceString) 
        // // … translation 
        // Return EncryptedString; 
    } }//

Executing Program

After Encryption excuted

When you set Method of Encryption and set Private Key, Press on Encrypt Button.

The program generates 2 parameters:

  1. Initialization Vector
  2. The Result ( encrypted string )

    After Encryption excuted

For decrypting a string, we need 2 parameters.
The program uses an Initialization Vector and result to decrypt a string.


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


About the Author

Amir Mehrabi-Jorshari
Software Developer (Senior) Omid Farda IT Company
Iran (Islamic Republic of) Iran (Islamic Republic of)
Master of Science in Computer Engineering

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
fatemeRokni10-May-13 8:10
memberfatemeRokni10-May-13 8:10 
AnswerRe: My vote of 5 Pin
Amir Mohammad Nasrollahi9-Aug-13 21:15
professionalAmir Mohammad Nasrollahi9-Aug-13 21:15 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey29-Mar-12 20:16
professionalManoj Kumar Choubey29-Mar-12 20:16 
QuestionHow to handle strings over more than one line Pin
georgebaker14-Mar-12 12:46
membergeorgebaker14-Mar-12 12:46 
AnswerRe: How to handle strings over more than one line Pin
Member 1175958712-Jun-15 12:53
memberMember 1175958712-Jun-15 12:53 
NewsIf you want to encrypt a whole filesystem Pin
jp7317-Nov-07 17:59
memberjp7317-Nov-07 17:59 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 6:52
memberTony Selke27-Sep-07 6:52 
Generalerror Pin
zhaojicheng9-Apr-07 21:33
memberzhaojicheng9-Apr-07 21:33 
GeneralRe: error Pin
Amir Mehrabi-Jorshari9-Apr-07 21:56
memberAmir Mehrabi-Jorshari9-Apr-07 21:56 
GeneralRandomness Pin
Jeffrey Walton5-Jan-07 3:09
memberJeffrey Walton5-Jan-07 3:09 
GeneralA FullFledged One Pin
Vasudevan Deepak Kumar4-Jan-07 19:51
memberVasudevan Deepak Kumar4-Jan-07 19:51 
GeneralEnglish Pin
Amir Mehrabi-Jorshari4-Jan-07 17:24
memberAmir Mehrabi-Jorshari4-Jan-07 17:24 
GeneralRe: English Pin
telwah16-Feb-09 6:16
membertelwah16-Feb-09 6:16 
Generalgrammar Pin
wk6333-Jan-07 8:14
memberwk6333-Jan-07 8:14 
GeneralRe: grammar Pin
MrBobIsMean30-Dec-08 9:43
memberMrBobIsMean30-Dec-08 9:43 
GeneralRe: grammar Pin
wk63330-Dec-08 9:59
memberwk63330-Dec-08 9:59 
GeneralClass Diagram Pin
Amir Mehrabi-Jorshari2-Jan-07 6:20
memberAmir Mehrabi-Jorshari2-Jan-07 6:20 
Generalsoftware Pin
econner2-Jan-07 4:39
membereconner2-Jan-07 4:39 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 30 Dec 2008
Article Copyright 2006 by Amir Mehrabi-Jorshari
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid