C # Common encryption methods

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Cryptography;
Using System.IO;

Namespace TRSLMS.Website.Common
{
<summary>
MD5 encryption
</summary>
public class Encrypt
{
private static MD5 MD5 = MD5. Create ();
Privatization constructors
Private Encrypt () {}
<summary>
Hashing a string using UTF8 encoding
</summary>
<param name= "sourcestring" > Encrypted string </param>
<returns> return encrypted string non-solvable </returns>
public static string SetEncryptMD5 (String sourcestring)
{
Try
{
byte[] Source = Md5.computehash (Encoding.UTF8.GetBytes (sourcestring));
StringBuilder Sbuilder = new StringBuilder ();
for (int i = 0; i < source. Length; i++)
{
Sbuilder.append (Source[i]. ToString ("X2"));
}
return sbuilder.tostring ();
}
catch (Exception)
{
Return "";
}
}

//**********************************************************************
Note that it is 8 characters, 64 bits
Const string key_64 = "Loubyuan";
Const string iv_64 = "19920923";
<summary>
Des encryption method
</summary>
<param name= "sourcestring" > Encrypted string </param>
<returns> post-encryption string </returns>
public static string Setencryptdes (String sourcestring)
{
Try
{
byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64);
byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64);
DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();
int i = cryptoprovider.keysize;
MemoryStream ms = new MemoryStream ();
CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createencryptor (Bykey, Byiv), cryptostreammode.write);
StreamWriter SW = new StreamWriter (CST);
Sw. Write (sourcestring);
Sw. Flush ();
Cst. FlushFinalBlock ();
Sw. Flush ();
Return convert.tobase64string (Ms. GetBuffer (), 0, (int) Ms. Length);
}
Catch {return "";}
}


<summary>
Des decryption
</summary>
<param name= "sourcestring" > Decrypt string </param>
<returns> post-decryption string </returns>
public static string Getdecryptdes (String sourcestring)
{
Try
{
byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64);
byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64);
byte[] Byenc = convert.frombase64string (sourcestring);
DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();
MemoryStream ms = new MemoryStream (BYENC);
CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createdecryptor (Bykey, Byiv), cryptostreammode.read);
StreamReader sr = new StreamReader (CST);
return Sr. ReadToEnd ();
}
Catch {return "";}
}

//**********************************************************************
Static byte[] Keyarray = UTF8Encoding.UTF8.GetBytes ("be4l4d5d0ff2djm2fpqmvy8flahbde4h");

//<summary>
//AES encryption Method
//</summary>
//<param name= "sourcestring" > Encrypted string </ Param>
//<returns> string </returns> after encryption,
public static string Setencryptaes (String sourcestring)
{
Try
{
byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (sourcestring);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createencryptor ();
byte[] resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return convert.tobase64string (resultarray, 0, resultarray.length);
}
Catch {return "";}
}

<summary>
AES Decryption
</summary>
<param name= "sourcestring" > Decrypt string </param>
<returns> post-decryption string </returns>
public static string Getdecryptaes (String sourcestring)
{
Try
{
byte[] Toencryptarray = convert.frombase64string (sourcestring);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createdecryptor ();
byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return UTF8Encoding.UTF8.GetString (resultarray);
}
Catch {return "";}
}

}
}

C # Common encryption methods

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.