|
using System;
using System.Security.Cryptography;
using System.Text;
namespace JobMorning
{
public class CryptoEncrypt
{
public static string encrypt (string original)
{
string encrypted, password;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
//create a secret password. the password is used to encrypt
//and decrypt strings. Without the password, the encrypted
//string cannot be decrypted and is just garbage. You must
//use the same password to decrypt an encrypted string as the
//string was originally encrypted with.
password = "urayasu";
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
hashmd5 = null;
//implement DES3 encryption
des = new TripleDESCryptoServiceProvider();
//the key is the secret password hash.
des.Key = pwdhash;
//the mode is the block cipher mode which is basically the
//details of how the encryption will work. There are several
//kinds of ciphers available in DES3 and they all have benefits
//and drawbacks. Here the Electronic Codebook cipher is used
//which means that a given bit of text is always encrypted
//exactly the same when the same password is used.
des.Mode = CipherMode.ECB; //CBC, CFB
//----- encrypt an un-encrypted string ------------
//the original string, which needs encrypted, must be in byte
//array form to work with the des3 class. everything will because
//most encryption works at the byte level so you'll find that
//the class takes in byte arrays and returns byte arrays and
//you'll be converting those arrays to strings.
buff = ASCIIEncoding.ASCII.GetBytes(original);
//encrypt the byte buffer representation of the original string
//and base64 encode the encrypted string. the reason the encrypted
//bytes are being base64 encoded as a string is the encryption will
//have created some weird characters in there. Base64 encoding
//provides a platform independent view of the encrypted string
//and can be sent as a plain text string to wherever.
encrypted = Convert.ToBase64String(
des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length)
);
//cleanup
des = null;
return encrypted;
}
}
/// <summary>
/// Summary description for CryptoDecrypt.
/// </summary>
public class CryptoDecrypt
{
public static string Decrypt (string encrypted)
{
string Decrypted, password;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
//create a secret password. the password is used to encrypt
//and Decrypt strings. Without the password, the encrypted
//string cannot be Decrypted and is just garbage. You must
//use the same password to Decrypt an encrypted string as the
//string was originally encrypted with.
password = "urayasu";
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
hashmd5 = null;
//implement DES3 encryption
des = new TripleDESCryptoServiceProvider();
//the key is the secret password hash.
des.Key = pwdhash;
//the mode is the block cipher mode which is basically the
//details of how the encryption will work. There are several
//kinds of ciphers available in DES3 and they all have benefits
//and drawbacks. Here the Electronic Codebook cipher is used
//which means that a given bit of text is always encrypted
//exactly the same when the same password is used.
des.Mode = CipherMode.ECB; //CBC, CFB
//whenever you Decrypt a string, you must do everything you
//did to encrypt the string, but in reverse order. To encrypt,
//first a normal string was des3 encrypted into a byte array
//and then base64 encoded for reliable transmission. So, to
//Decrypt this string, first the base64 encoded string must be
//decoded so that just the encrypted byte array remains.
buff = Convert.FromBase64String(encrypted);
//Decrypt DES 3 encrypted byte buffer and return ASCII string
Decrypted = ASCIIEncoding.ASCII.GetString(
des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length)
);
//cleanup
des = null;
return Decrypted;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.