Class of the commonly used cryptographic strings, transferred from Osleague Ah, author: bigeagle

Source: Internet
Author: User
Tags array to string base64 decrypt md5 valid
Encryption | string using System;
Using System.Security;
Using System.Security.Cryptography;
Using System.Diagnostics;
Using System.Web;
Using System.Text;

Namespace Bigeagle.util
{
<summary>
An encryption class
<br>Author:Bigeagle@163.net</br>
<br>Date:2001/09/25</br>
&LT;BR&GT;HISTORY:2001/10/25 finished</br>
</summary>
<remarks>
Encapsulation of commonly used encryption algorithms
</remarks>
public Class cryptography
{

<summary>
MD5 encrypt the specified string
</summary>
<param name= "A_strvalue" > string to encrypt </param>
<returns> the encrypted string </returns>
public static string Encryptmd5string (String a_strvalue)
{
#if DEBUG
Debug.Assert (A_strvalue.trim ()!= "", "empty string", "empty string does not need encryption");
#endif//debug

Convert into ByteArray
Byte[] HBA = ((HashAlgorithm) cryptoconfig.createfromname ("MD5")).
ComputeHash (Stringtobytearray (A_strvalue));

Return bytearraytostring (HBA);
}

<summary>
Determine if two encrypted strings are the same
</summary>
<param name= "A_STR1" ></param>
<param name= "A_STR2" ></param>
<returns> if the same return true </returns>
public static bool Issame (string a_str1, String a_str2)
{
Byte[] B1 = Stringtobytearray (A_STR1);
byte[] B2 = Stringtobytearray (A_STR2);
if (B1. Length!= B2. Length)
{
return false;
}

for (int i = 0; I < B1. Length; i + +)
{
if (B1[i]!= b2[i])
{
return false;
}
}

return true;
}

<summary>
Convert string to Byte tree Group
</summary>
<param name= "S" > string to be converted </param>
<returns> conversion of byte array </returns>
public static byte[] Stringtobytearray (String s)
{
/*
char[] CA = S.tochararray ();
byte[] ba = new BYTE[CA. Length];
for (int i=0; i<ba. Length; i++) Ba[i] = (Byte) ca[i];
Return ba;*/

return Encoding.UTF8.GetBytes (s);
}

<summary>
Convert byte array to string
</summary>
<param name= "a_arrbyte" >byte array </param>
<returns> string </returns>
public static string ByteArrayToString (byte[] a_arrbyte)
{
/*
char[] CA = new Char[a_arrbyte.length];
for (int i = 0; i < a_arrbyte.length i + +)
{
Result + = (char) a_arrbyte[i];
}*/

Return Encoding.UTF8.GetString (A_arrbyte);
}


<summary>
3DES encrypted string
</summary>
<param name= "A_strstring" > string to encrypt </param>
<param name= "A_strkey" > Key </param>
<returns> encoded string </returns> after encryption and base64
<remarks> static method, using default ASCII encoding </remarks>
public static string Encrypt3des (String a_strstring, String a_strkey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider ();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider ();

Des. Key = Hashmd5.computehash (ASCIIEncoding.ASCII.GetBytes (A_strkey));
Des. Mode = CIPHERMODE.ECB;

ICryptoTransform desencrypt = DES. CreateEncryptor ();

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes (a_strstring);
Return Convert.tobase64string (Desencrypt.transformfinalblock
(Buffer, 0, buffer.length));
}//end method

<summary>
3DES encrypted string
</summary>
<param name= "A_strstring" > string to encrypt </param>
<param name= "A_strkey" > Key </param>
<param name= "encoding" > Coding mode </param>
<returns> encoded string </returns> after encryption and base63
<remarks> overload, specifying the encoding method </remarks>
public static string Encrypt3des (String a_strstring, String a_strkey, Encoding Encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider ();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider ();

Des. Key = Hashmd5.computehash (encoding. GetBytes (A_strkey));
Des. Mode = CIPHERMODE.ECB;

ICryptoTransform desencrypt = DES. CreateEncryptor ();

byte[] Buffer = encoding. GetBytes (a_strstring);
Return Convert.tobase64string (Desencrypt.transformfinalblock
(Buffer, 0, buffer.length));
}


<summary>
3DES decryption string
</summary>
<param name= "A_strstring" > String to Decrypt </param>
<param name= "A_strkey" > Key </param>
<returns> after decryption of the string </returns>
<exception cref= "" > Key error </exception>
<remarks> static method, using default ASCII encoding </remarks>
public static string Decrypt3des (String a_strstring, String a_strkey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider ();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider ();

Des. Key = Hashmd5.computehash (ASCIIEncoding.ASCII.GetBytes (A_strkey));
Des. Mode = CIPHERMODE.ECB;

ICryptoTransform desdecrypt = DES. CreateDecryptor ();

string result = "";
Try
{
byte[] Buffer = convert.frombase64string (a_strstring);
result = ASCIIEncoding.ASCII.GetString (desdecrypt.transformfinalblock
(Buffer, 0, buffer.length));
}
catch (Exception e)
{
#if DEBUG
Console.WriteLine ("Error: {0}", e);
#endif//debug
Throw (New Exception ("Invalid Key or input string is not a valid Base64 string", E));
}

return result;
}//end method

<summary>
3DES decryption string
</summary>
<param name= "A_strstring" > String to Decrypt </param>
<param name= "A_strkey" > Key </param>
<param name= "encoding" > Coding mode </param>
<returns> after decryption of the string </returns>
<exception cref= "" > Key error </exception>
<remarks> static method, specifying encoding method </remarks>
public static string Decrypt3des (String a_strstring, String a_strkey, Encoding Encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider ();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider ();

Des. Key = Hashmd5.computehash (encoding. GetBytes (A_strkey));
Des. Mode = CIPHERMODE.ECB;

ICryptoTransform desdecrypt = DES. CreateDecryptor ();

string result = "";
Try
{
byte[] Buffer = convert.frombase64string (a_strstring);
result = Encoding. GetString (Desdecrypt.transformfinalblock
(Buffer, 0, buffer.length));
}
catch (Exception e)
{
#if DEBUG
Console.WriteLine ("Error: {0}", e);
#endif//debug
Throw (New Exception ("Invalid Key or input string is not a valid Base64 string", E));
}

return result;
}//end method


}
}


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.