. Net encryption and decryption class

Source: Internet
Author: User

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

Namespace CommHelper
{
Public class CEntrypt
{
Private static string sKeyForEncryptTitle;

Public CEntrypt ()
{}

# Region decrypts Base64
/// <Summary>
/// Decrypt Base64
/// </Summary>
/// <Param name = "asContent"> </param>
/// <Returns> </returns>
Public static string Base64TextUTF8Decode (string asContent)
{
Try
{
Byte [] bytes = Convert. FromBase64String (asContent );
UTF8Encoding encoding = new UTF8Encoding ();
Return encoding. GetString (bytes );
}
Catch
{
Return "";
}
}
# Endregion

# Region encrypted Base64
/// <Summary>
/// Encrypted Base64
/// </Summary>
/// <Param name = "asText"> </param>
/// <Returns> </returns>
Public static string Base64UTF8Encode (string asText)
{
Try
{
String s = asText;
UTF8Encoding encoding = new UTF8Encoding ();
Return Convert. ToBase64String (encoding. GetBytes (s ));
}
Catch
{
Return "";
}
}
# Endregion

# Region UTF8 decryption
/// <Summary>
/// Decrypt UTF8
/// </Summary>
/// <Param name = "asEnCodeText"> </param>
/// <Returns> </returns>
Public static string DecodeUTF8Text (string asEnCodeText)
{
Try
{
Byte [] bytes = Convert. FromBase64String (asEnCodeText );
UTF8Encoding encoding = new UTF8Encoding ();
Return encoding. GetString (bytes );
}
Catch
{
Return "";
}
}
# Endregion

# Region static encryption UTF8
/// <Summary>
/// Encrypt UTF8
/// </Summary>
/// <Param name = "asDecodeText"> </param>
/// <Returns> </returns>
Public string EnCodeUTF8Text (string asDecodeText)
{
Try
{
UTF8Encoding encoding = new UTF8Encoding ();
Return Convert. ToBase64String (encoding. GetBytes (asDecodeText ));
}
Catch
{
Return "";
}
}
# Endregion

# Region EncryptHelper Encryption

/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "Text"> </param>
/// <Returns> </returns>
Public static string Encrypt (string Text)
{
Return Encrypt (Text, "EncryptHelper ");
}
/// <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 EncryptHelper decryption

/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "Text"> </param>
/// <Returns> </returns>
Public static string Decrypt (string Text)
{
Return Decrypt (Text, "EncryptHelper ");
}
/// <Summary>
/// Decrypt 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

# Region DeCodeID decryption
Public static string DecodeID (string Encode)
{
If (Encode = "")
{
Return "";
}
Int length = Encode. Length;
If (length <5)
{
Return "";
}
Int num2 = Convert. ToInt32 (Encode. Substring (2, 1 ));
Int num3 = Convert. ToInt32 (Encode. Substring (4, 1 ));
Int startIndex = (num2 * 2) + (num3 * 2) + 7;
If (length <(startIndex + 20 ))
{
Return "";
}
Num2 = (Convert. ToInt32 (Encode. Substring (startIndex, 1) * 10) + Convert. ToInt32 (Encode. Substring (startIndex + 1, 1 ));
StartIndex = (startIndex + 2)-1;
String str = "";
For (int I = 0; I <num2; I ++)
{
StartIndex = (startIndex + 3) + 1;
Str = str + Encode. Substring (startIndex, 1 );
}
Return str;
}
# Endregion

# Region EncodeID Encryption
Public static string EncodeID (string ID)
{
Int num6;
Int num = (DateTime. Now. Hour * DateTime. Now. Minute) * DateTime. Now. Second;
If (num & lt; 100)
{
Num = (num + 0x70) * 0x25;
}
String str = "";
For (num6 = 1; num6 <100; num6 ++)
{
Int num2 = num * (num6% 50 );
Str = str + num2.ToString (). Trim ();
If (str. Length> 120)
{
Break;
}
}
Int num3 = Convert. ToInt32 (str. Substring (2, 1 ));
Int num4 = Convert. ToInt32 (str. Substring (4, 1 ));
Int length = (num3 * 2) + (num4 * 2) + 7;
Num3 = ID. Length;
String str2 = str. Substring (0, length );
If (num3 <10)
{
Str2 = str2 + "0 ";
}
Str2 = str2 + num3.ToString (). Trim ();
For (num6 = 0; num6 <num3; num6 ++)
{
Str2 = str2 + str. Substring (1 + (num6 * 3), 3) + ID. Substring (num6, 1 );
}
Num3 = str2.Length;
Try
{
Str2 = str2 + str. Substring (20,110-num3 );
}
Catch
{
}
Return str2;
}
# Endregion

# Region DecodeURL decryption
Public static string DecodeURL (string NameValueStr)
{
Uint num;
If (NameValueStr = null) | (NameValueStr = ""))
{
Return "";
}
String str = NameValueStr. trim (). replace ("X", "1U1 "). replace ("Y", "2U2 "). replace ("Z", "3U3 "). replace ("V", "4U5 "). replace ("W", "7U6 "). replace ("O", "8U7 "). replace ("P", "0U8 "). replace ("Q", "0U9 "). replace ("R", "1U0 "). replace ("S", "2U4 "). replace ("x", "U1 "). replace ("y", "U2 "). replace ("z", "U3 "). replace ("u", "U4 "). replace ("v", "U5 "). replace ("w", "U6 "). replace ("o", "U7 "). replace ("p", "U8 "). replace ("q", "U9 "). replace ("r", "U0 ");
String str2 = "";
String str3 = "";
For (int I = 1; I <str. Length; I ++)
{
If (str [I] = 'U ')
{
If (str2! = "")
{
Num = Convert. ToUInt32 (str2 );
Str3 = str3 + Convert. ToChar (num). ToString (). Trim ();
Str2 = "";
}
}
Else
{
Char ch2 = str [I];
Str2 = str2 + ch2.ToString (). Trim ();
}
}
If (str2! = "")
{
Num = Convert. ToUInt32 (str2 );
Str3 = str3 + Convert. ToChar (num). ToString (). Trim ();
}
Return str3;
}
# Endregion

# Region EncodeURL Encryption
Public static string EncodeURL (string NameValueStr)
{
If (NameValueStr = null) | (NameValueStr = ""))
{
Return "";
}
String str = "";
NameValueStr = NameValueStr. Trim ();
For (int I = 0; I <NameValueStr. Length; I ++)
{
Str = str + "U" + Convert. ToUInt16 (NameValueStr [I]). ToString (). Trim ();
}
Return str. replace ("1U1", "X "). replace ("2U2", "Y "). replace ("3U3", "Z "). replace ("4U5", "V "). replace ("7U6", "W "). replace ("8U7", "O "). replace ("0U8", "P "). replace ("0U9", "Q "). replace ("1U0", "R "). replace ("2U4", "S "). replace ("U1", "x "). replace ("U2", "y "). replace ("U3", "z "). replace ("U4", "u "). replace ("U5", "v "). replace ("U6", "w "). replace ("U7", "o "). replace ("U8", "p "). replace ("U9", "q "). replace ("U0", "r ");
}
# Endregion
}
}

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.