This is a basic version of encrypt-decrypt function…we used this from one of the .NET sites, since i am not able to find teh reference link , iam posting it here so that others can use it…
public class MyEnctDect
{
private Byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
private Byte[] iv = { 65, 110, 68, 26, 69, 178, 200, 219 };
public string Encrypt(string plainText)
{
//’ Declare a UTF8Encoding object so we may use the GetByte
//’ method to transform the plainText into a Byte array.
String outString;
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputInBytes = utf8encoder.GetBytes(plainText);
//’ Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
//’ The ICryptTransform interface uses the TripleDES
//’ crypt provider along with encryption key and init vector
//’ information
ICryptoTransform cryptoTransform = tdesProvider.CreateEncryptor(key, iv);
//’ All cryptographic functions need a stream to output the
//’ encrypted information. Here we declare a memory stream
//’ for this purpose.
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
//’ Write the encrypted information to the stream. Flush the information
//’ when done to ensure everything is out of the buffer.
cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;
//’ Read the stream back into a Byte array and return it to the calling
//’ method.
// Dim result(encryptedStream.Length – 1) As Byte
byte[] result = new byte[encryptedStream.Length];
encryptedStream.Read(result, 0, (int)encryptedStream.Length);
cryptStream.Close();
//’test section
//’Convert to / fro the memory stream to a base64 string
//’compare decText to result
outString = System.Text.ASCIIEncoding.Default.GetString(result);
string encText = Convert.ToBase64String(encryptedStream.ToArray());
byte[] decText = Convert.FromBase64String(encText);
string sDec = Decrypt(decText);
return outString;
}
public String Decrypt(Byte[] inputInBytes)
{
//’ UTFEncoding is used to transform the decrypted Byte Array
//’ information back into a string.
UTF8Encoding utf8encoder = new UTF8Encoding();
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
//’ As before we must provide the encryption/decryption key along with
//’ the init vector.
ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(key, iv);
//’ Provide a memory stream to decrypt information into
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write);
cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;
//’ Read the memory stream and convert it back into a string
Byte[] result = new byte[decryptedStream.Length];
decryptedStream.Read(result, 0, (int)decryptedStream.Length);
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result);
}
}
usage
MyEnctDect classDeclartion = new MyEnctDect();
classDeclartion.Encrypt(text);
classDeclartion.Decrypt(bytearray);
Its a good one for beginners, there are other methods available as well
Posted by consoleart