MD5 encryption and decryption
Using system;
Using system. text;
Using system. IO;
Using system. Security. cryptography;
Class class1
{
Static void main ()
{
Console. writeline ("encrypt string ...");
Txtkey = "tkggrmbervc ="; // sets the encryption key.
Btnkeygen ();
Console. writeline ("Encrypt Key: {0}", txtkey );
Txtiv = "kl7zgtm1dvq ="; // sets encryption IV
Btnivgen ();
Console. writeline ("encrypt IV: {0}", txtiv );
Console. writeline ();
String txtencrypted = encryptstring ("test ");
Console. writeline ("encrypt string: {0}", txtencrypted );
String txtoriginal = decryptstring (txtencrypted );
Console. writeline ("decrypt string: {0}", txtoriginal );
}
Private Static metrics ricalgorithm MCSP;
Private Static string txtkey;
Private Static string txtiv;
Private Static void btnkeygen ()
{
MCSP = setenc ();
Byte [] byt2 = convert. frombase64string (txtkey );
MCSP. Key = byt2;
}
Private Static void btnivgen ()
{
Byte [] byt2 = convert. frombase64string (txtiv );
MCSP. IV = byt2;
}
Private Static string encryptstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );
Byt = encoding. utf8.getbytes (value );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return convert. tobase64string (Ms. toarray ());
}
Private Static string decryptstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
Byt = convert. frombase64string (value );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return encoding. utf8.getstring (Ms. toarray ());
}
Private Static metrics ricalgorithm setenc ()
{
Return new descryptoserviceprovider ();
}
}