asp.net encrypthelper encryption Help class _ Practical tips

Source: Internet
Author: User
Tags md5 md5 encryption
Copy Code code as follows:

/**************************************************
* All rights reserved: Mr_sheng
* FileName: EncryptHelper.cs
* File Description:
* Type description: Encrypthelper encryption Help Class
* Authorization statement:
* This procedure is free software;
* You may redistribute and/or modify this procedure in accordance with the GPL V3 license Terms issued by the Free Software Foundation;
* This procedure is issued for the purpose of use, but does not bear any liability for security;
* There is no implied warranty of merchantability or fitness for a particular purpose.
* For details please refer to the GNU General Public License V3 (see License.txt file).
* Version history:
* v2.0.0 Mr_sheng 2009-09-09 Revision
*
***************************************************/
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Cryptography;

Namespace Sheng.common
{
<summary>
Encryption Help Class
</summary>
public class Encrypthelper
{
<summary>
MD5 encryption
</summary>
<param name= "str" ></param>
<returns></returns>
public static string md5decryptstring (String str)
{
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider ();
byte[] Md5source = System.Text.Encoding.UTF8.GetBytes (str);
byte[] Md5out = Md5.computehash (Md5source);
Return convert.tobase64string (md5out);
}

<summary>
Des encrypted string
</summary>
<param name= "sinputstring" > Input character </param>
<param name= "SKey" >Key</param>
<returns> Encryption Results </returns>
public string desencryptstring (string sinputstring, String SKey)
{
Try
{
byte[] data = Encoding.Default.GetBytes (sinputstring);
Byte[] result;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider ();
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey); Secret key
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey); Initializing vectors
ICryptoTransform desencrypt = DES. CreateEncryptor (); Cryptographic object
result = Desencrypt. TransformFinalBlock (data, 0, data.) Length); Converts a specified range of specified byte arrays
return bitconverter.tostring (Result);
}
catch (Exception ex)
{
Ex. Message = "DES encryption exception";
Throw ex;
}
}

<summary>
Des decryption string
</summary>
<param name= "sinputstring" > Input character </param>
<param name= "SKey" >Key</param>
<returns> Decryption Results </returns>
public string desdecryptstring (string sinputstring, String SKey)
{
Try
{
Converts a string to a byte array
string[] Sinput = Sinputstring.split ("-"). ToCharArray ());
byte[] data = new Byte[sinput.length];
Byte[] result;
for (int i = 0; i < sinput.length; i++)
{
Data[i] = byte. Parse (Sinput[i], System.Globalization.NumberStyles.HexNumber);
}

DESCryptoServiceProvider DES = new DESCryptoServiceProvider ();
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
ICryptoTransform desencrypt = DES. CreateDecryptor ();
result = Desencrypt. TransformFinalBlock (data, 0, data.) Length);
return Encoding.Default.GetString (Result);
}
catch (Exception ex)
{
Ex. Message = "Des decryption exception";
Throw ex;
}
}
}
}
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.