An example of DES encryption/decryption class in asp.net

Source: Internet
Author: User
Tags decrypt md5

DES encryption function

The code is as follows Copy Code

Default key vector
private static byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
<summary>
Des encrypted string
</summary>
<param name= "encryptstring" > Strings to be encrypted </param>
<param name= "Encryptkey" > Encryption key required for 8-bit </param>
<returns> encryption successfully returned the encrypted string, failed to return the source string </returns>
public static string Encryptdes (String encryptstring, String encryptkey)
{
Try
{
byte[] Rgbkey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));
byte[] Rgbiv = Keys;
byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, Dcsp.createencryptor (Rgbkey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return convert.tobase64string (Mstream.toarray ());
}
Catch
{
return encryptstring;
}
}

<summary>
Des decryption string
</summary>
<param name= "decryptstring" > Strings to be decrypted </param>
<param name= "Decryptkey" > Decryption key, required 8-bit, same as encryption key </param>
<returns> decryption successfully returned decrypted string, failed back source string </returns>
public static string Decryptdes (String decryptstring, String decryptkey)
{
Try
{
byte[] Rgbkey = Encoding.UTF8.GetBytes (Decryptkey);
byte[] Rgbiv = Keys;
byte[] Inputbytearray = convert.frombase64string (decryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, DCSP. CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return Encoding.UTF8.GetString (Mstream.toarray ());
}
Catch
{
return decryptstring;
}
}

Now we put the password and decryption together.

The code is as follows Copy Code

Using System;
Using System.Security.Cryptography;
Using System.Text;
<summary>
DES encryption/Decryption class.
Litianping
</summary>
public class Desencrypt
{
Public Desencrypt ()
{
}

#region ======== Encryption ========

<summary>
Encryption
</summary>
<param name= "Text" ></param>
<returns></returns>
public static string Encrypt (String Text)
{
Return Encrypt (Text, "Maticsoft");
}
<summary>
Encrypt data
</summary>
<param name= "Text" ></param>
<param name= "SKey" ></param>
<returns></returns>
public static string Encrypt (String Text, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte[] Inputbytearray;
Inputbytearray = Encoding.Default.GetBytes (Text);
Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8));
DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms.) ToArray ())
{
Ret. AppendFormat ("{0:x2}", b);
}
return ret. ToString ();
}

#endregion

#region ======== Decryption ========


<summary>
Decrypt
</summary>
<param name= "Text" ></param>
<returns></returns>
public static string Decrypt (String Text)
{
Return Decrypt (Text, "Maticsoft");
}
<summary>
Decrypting data
</summary>
<param name= "Text" ></param>
<param name= "SKey" ></param>
<returns></returns>
public static string Decrypt (String Text, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
int Len;
len = TEXT.LENGTH/2;
byte[] Inputbytearray = new Byte[len];
int x, I;
for (x = 0; x < len X + +)
{
i = Convert.ToInt32 (text.substring (x * 2, 2), 16);
INPUTBYTEARRAY[X] = (byte) i;
}
Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8));
DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
Return Encoding.Default.GetString (Ms. ToArray ());
}

#endregion


}

Related Article

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.