Click here to Skip to main content
6,306,412 members and growing! (17,112 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Intermediate License: The Code Project Open License (CPOL)

Encrypt and Decrypt an string

By Amir Mehrabi J.

In this article you can encrypt a string and decrypt it again
Windows, Visual Studio, Dev
Posted:28 Dec 2006
Updated:29 Dec 2008
Views:43,549
Bookmarked:64 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
22 votes for this article.
Popularity: 3.80 Rating: 2.83 out of 5
7 votes, 31.8%
1

2
4 votes, 18.2%
3
5 votes, 22.7%
4
6 votes, 27.3%
5

Sample Image - Encrypt_an_string1.gif

Introduction

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

 

Background

This article explain 4 method to encrypt a string In .Net 2.0 there is 4 popular method to encrypt an string:
- DES
- RC2
- Rijindeal
- Triple DES

Note: for all of these method we are some required parameter
- Specify an Method
- Use a Private Key

What is Private Key?


Private Key is an string with specify Length
For DES method the length of Private key string must be 8 character
For RC2 method the length of Private key string must be Minimum 7 Character

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, and 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 are 2 class
- 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; 
    } }//

Excuting Prgram

After Encryption excuted

When you set Method of Encryption and set Private Key Press on Encrypt Button
The program generate 2 parameter
1: Initialization Vector
2: The Result ( encrypted string )


After Encryption excuted
 

For Decrypt a string we need 2 Parameter
The program use Initialization Vector and result to decrypt a string

License

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

About the Author

Amir Mehrabi J.


Member
Software Enginner;
--------------------
Programming in C#.NET , VB.NET , VB6 , ASP.NET / ASP
AND SQL SERVER 2000
Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
NewsIf you want to encrypt a whole filesystem Pinmemberjp73118:59 7 Nov '07  
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke7:52 27 Sep '07  
Generalerror Pinmemberzhaojicheng22:33 9 Apr '07  
GeneralRe: error PinmemberAmir Mehrabi J.22:56 9 Apr '07  
GeneralRandomness PinmemberJeffrey Walton4:09 5 Jan '07  
GeneralA FullFledged One PinmemberVasudevan Deepak Kumar20:51 4 Jan '07  
GeneralEnglish PinmemberAmir Mehrabi J.18:24 4 Jan '07  
GeneralRe: English Pinmembertelwah7:16 16 Feb '09  
Generalgrammar Pinmemberwk6339:14 3 Jan '07  
GeneralRe: grammar PinmemberMrBobIsMean10:43 30 Dec '08  
GeneralRe: grammar Pinmemberwk63310:59 30 Dec '08  
GeneralClass Diagram PinmemberAmir Mehrabi J.7:20 2 Jan '07  
Generalsoftware Pinmembereconner5:39 2 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Dec 2008
Editor:
Copyright 2006 by Amir Mehrabi J.
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project