![]() |
General Programming »
Algorithms & Recipes »
General
Intermediate
License: The Code Project Open License (CPOL)
Encrypt and Decrypt an stringBy Amir Mehrabi J.In this article you can encrypt a string and decrypt it again |
Windows, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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
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
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...)
For Implementing encrypt a string we are 2 class
- EncryptEngine
- Encryptor
//
// 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;
} }//
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 )
For Decrypt a string we need 2 Parameter
The program use Initialization Vector and result to decrypt a string
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Dec 2008 Editor: |
Copyright 2006 by Amir Mehrabi J. Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |