Saturday, May 30, 2009

Symmetric Encryption Class

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Encryption;

namespace Encryption {

public class TripleDESEncryption {

public static string EncryptData(string data, out byte[] desKey, out byte[] desIV) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the
//same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees
//output of two identical plaintext blocks are different.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(), CryptoStreamMode.Write);

//Assign our crypto-generated key and iv values to our output arguments
desKey = des.Key; desIV = des.IV;
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}

public static string EncryptData(byte[] desKey, byte[] desIV, string data) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the
//same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees
//output of two identical plaintext blocks are different.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}

public static string DecryptData(string data, byte[] desKey, byte[] desIV) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to decrypt our data. In order for the ciphertext to be
//successfully decrypted, the exact same key and iv must be used when initially encryted.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}
}
}

class CipherText {

static void Main() {
byte[] key; byte[] iv;
string password = "secret";
string cif = TripleDESEncryption.EncryptData(password, out key, out iv);
Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, iv));

//NOTE: Key and IVector must be 16 bytes each
//byte[] key = UnicodeEncoding.Unicode.GetBytes("cornhle");
//string cif = TripleDESEncryption.EncryptData(key, key, password);
//Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, key));
}
}

1 comment:

  1. Dear Sir,

    I hope you are doing well. I got this email address from one of your contribution web site. I have launched a web site www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter.
    Looking forward to hearing from you and hope you will join with us soon.

    Thank you
    RRaveen
    Founder CodeGain.com

    ReplyDelete