Cryptography, a class of encryption and decryption algorithms written in C #

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.