A Class of encryption and decryption algorithms:
Code
Using system;
Using system. IO;
Using system. Security. cryptography;
Using system. text;
Namespace does T. Business. userprivilege
{
/// <Summary>
/// Symmtric crypto
/// </Summary>
Public class RijndaelCrypto
{
Private encryption ricalgorithm _ objCryptoService;
Private string _ strKey = "Guz (% & hj7x89H $ yuBI0456FtmaT5 & fvHUFCy76 * h % (HilJ $ lhj! Y6 & (* jkP87jH7 ";
Private string _ strIV = "E4ghj * Ghg7! RNIfb & 95GUY86GfghUb # er57HBh (u % g6HJ ($ jhWk7 &! Hg4ui % $ hjk ";
/// <Summary>
/// Conctructor
/// </Summary>
Public RijndaelCrypto (string key, string IV)
{
_ ObjCryptoService = new RijndaelManaged ();
_ ObjCryptoService = new TripleDESCryptoServiceProvider ();
_ StrKey = key;
_ StrIV = IV;
}
Public RijndaelCrypto ()
{
_ ObjCryptoService = new RijndaelManaged ();
_ ObjCryptoService = new TripleDESCryptoServiceProvider ();
}
/// <Summary>
/// Get the key
/// </Summary>
/// <Returns> key </returns>
Private byte [] GetLegalKey ()
{
String stemp = _ strkey;
_ Objcryptoservice. generatekey ();
Byte [] byttemp = _ objcryptoservice. Key;
Int keylength = byttemp. length;
If (stemp. length> keylength)
Stemp = stemp. substring (0, keylength );
Else if (stemp. Length <keylength)
STemp = sTemp. PadRight (KeyLength ,'');
Return ASCIIEncoding. ASCII. GetBytes (sTemp );
}
/// <Summary>
/// Initialize IV
/// </Summary>
/// <Returns> Initialize IV </returns>
Private byte [] GetLegalIV ()
{
_ Objcryptoservice. generateiv ();
Byte [] byttemp = _ objcryptoservice. IV;
Int ivlength = byttemp. length;
If (_ striv. length> ivlength)
_ Striv = _ striv. substring (0, ivlength );
Else if (_ striv. Length <ivlength)
_ Striv = _ striv. padright (ivlength ,'');
Return asciiencoding. ASCII. getbytes (_ striv );
}
/// <Summary>
/// Encrypto
/// </Summary>
/// <Param name = "Source"> the source string need to encrypto </param>
/// <Returns> The string after crypto </returns>
Public string Encrypto (string Source)
{
Byte [] bytIn = UTF8Encoding. UTF8.GetBytes (Source );
MemoryStream MS = new MemoryStream ();
_ ObjCryptoService. Key = GetLegalKey ();
_ ObjCryptoService. IV = GetLegalIV ();
ICryptoTransform encrypto = _ objCryptoService. CreateEncryptor ();
Using (CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. Write ))
{
Cs. Write (bytIn, 0, bytIn. Length );
Cs. FlushFinalBlock ();
Ms. Close ();
Byte [] bytout = Ms. toarray ();
String strret = convert. tobase64string (bytout );
CS. Clear ();
Return strret;
}
}
/// <Summary>
/// Decrypto
/// </Summary>
/// <Param name = "Source"> the srouce need to decrypto </param>
/// <Returns> the result string after decrypto </returns>
Public String decrypto (string source)
{
Byte [] bytin = convert. frombase64string (source );
Using (memorystream MS = new memorystream (bytin, 0, bytin. Length ))
{
_ ObjCryptoService. Key = GetLegalKey ();
_ ObjCryptoService. IV = GetLegalIV ();
ICryptoTransform encrypto = _ objCryptoService. CreateDecryptor ();
CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. Read );
StreamReader sr = new StreamReader (cs );
String strRet = sr. ReadToEnd ();
Cs. Clear ();
Return strRet;
}
}
}
}
The usage is as follows:
Code
Public static string encryptopassword (string PWD)
{
Rijndaelcrypto Rijndael = new rijndaelcrypto ();
Return Rijndael. encrypto (PWD );
}
Public static string DecryptoPassword (string CryptoPwd)
{
RijndaelCrypto rijndael = new RijndaelCrypto ();
Return rijndael. Decrypto (CryptoPwd );
}