C # source code of common encryption algorithms such as MD5, SHA1, SHA256, and SHA512

Source: Internet
Author: User

Copy codeThe Code is as follows: using System;
Using System. IO;
Using System. Data;
Using System. Text;
Using System. Diagnostics;
Using System. Security;
Using System. Security. Cryptography;
/**//*
* The. Net Framework supports a wide range of databases provided by CLR. Only a small amount of code is required to implement encryption algorithms that are hard to implement in old languages such as C. This class implements some common secret algorithms for your reference. The MD5 Algorithm returns the Int ToString. For algorithms for returning alphanumeric results, see previous Blog articles.
*/
Digital Processing of namespace Archives
{
/** // <Summary>
/// Class Name: HashEncrypt
/// Function: Hash the input string and return the string encrypted by the Hash algorithm.
/// Attribute: [none]
/// Parameters of the constructor:
/// IsReturnNum: whether to return the Byte code of the encrypted character
/// IsCaseSensitive: case sensitive.
/// Method: This class provides algorithms such as MD5, SHA1, SHA256, and SHA512, and the length of the encrypted string increases sequentially.
/// </Summary>
Public class HashEncrypt
{
// Private string strIN;
Private bool isReturnNum;
Private bool isCaseSensitive;
/** // <Summary>
/// Class initialization. This class provides algorithms such as MD5, SHA1, SHA256, and SHA512, and the length of the encrypted string increases sequentially.
/// </Summary>
/// <Param name = "IsCaseSensitive"> case sensitive </param>
/// <Param name = "IsReturnNum"> whether to return the Byte code of the encrypted character </param>
Public HashEncrypt (bool IsCaseSensitive, bool IsReturnNum)
{
This. isReturnNum = IsReturnNum;
This. isCaseSensitive = IsCaseSensitive;
}
Private string getstrIN (string strIN)
{
// String strIN = strIN;
If (strIN. Length = 0)
{
StrIN = "~ NULL ~ ";
}
If (isCaseSensitive = false)
{
StrIN = strIN. ToUpper ();
}
Return strIN;
}
Public string MD5Encrypt (string strIN)
{
// String strIN = getstrIN (strIN );
Byte [] tmpByte;
MD5 md5 = new MD5CryptoServiceProvider ();
TmpByte = md5.ComputeHash (GetKeyByteArray (getstrIN (strIN )));
Md5.Clear ();
Return GetStringValue (tmpByte );
}
Public string SHA1Encrypt (string strIN)
{
// String strIN = getstrIN (strIN );
Byte [] tmpByte;
SHA1 sha1 = new SHA1CryptoServiceProvider ();
TmpByte = sha1.ComputeHash (GetKeyByteArray (strIN ));
Sha1.Clear ();
Return GetStringValue (tmpByte );
}
Public string SHA256Encrypt (string strIN)
{
// String strIN = getstrIN (strIN );
Byte [] tmpByte;
SHA256 sha256 = new SHA256Managed ();
TmpByte = sha256.ComputeHash (GetKeyByteArray (strIN ));
Sha256.Clear ();
Return GetStringValue (tmpByte );
}
Public string SHA512Encrypt (string strIN)
{
// String strIN = getstrIN (strIN );
Byte [] tmpByte;
SHA512 sha512 = new SHA512Managed ();
TmpByte = sha512.ComputeHash (GetKeyByteArray (strIN ));
Sha512.Clear ();
Return GetStringValue (tmpByte );
}
/** // <Summary>
/// Use DES encryption (Added by niehl 2005-4-6)
/// </Summary>
/// <Param name = "originalValue"> string to be encrypted </param>
/// <Param name = "key"> key (maximum length 8) </param>
/// <Param name = "IV"> initialization vector (maximum length 8) </param>
/// <Returns> encrypted string </returns>
Public string DESEncrypt (string originalValue, string key, string IV)
{
// Process the key and IV into 8 characters
Key + = "12345678 ";
IV + = "12345678 ";
Key = key. Substring (0, 8 );
IV = IV. Substring (0, 8 );
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
Sa = new DESCryptoServiceProvider ();
Sa. Key = Encoding. UTF8.GetBytes (key );
Sa. IV = Encoding. UTF8.GetBytes (IV );
Ct = sa. CreateEncryptor ();
Byt = Encoding. UTF8.GetBytes (originalValue );
MS = new MemoryStream ();
Cs = new CryptoStream (MS, ct, CryptoStreamMode. Write );
Cs. Write (byt, 0, byt. Length );
Cs. FlushFinalBlock ();
Cs. Close ();
Return Convert. ToBase64String (ms. ToArray ());
}
Public string DESEncrypt (string originalValue, string key)
{
Return DESEncrypt (originalValue, key, key );
}
/** // <Summary>
/// Use DES for decryption (Added by niehl 2005-4-6)
/// </Summary>
/// <Param name = "encryptedValue"> string to be decrypted </param>
/// <Param name = "key"> key (maximum length 8) </param>
/// <Param name = "IV"> m initialization vector (maximum length 8) </param>
/// <Returns> decrypted string </returns>
Public string DESDecrypt (string encryptedValue, string key, string IV)
{
// Process the key and IV into 8 characters
Key + = "12345678 ";
IV + = "12345678 ";
Key = key. Substring (0, 8 );
IV = IV. Substring (0, 8 );
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
Sa = new DESCryptoServiceProvider ();
Sa. Key = Encoding. UTF8.GetBytes (key );
Sa. IV = Encoding. UTF8.GetBytes (IV );
Ct = sa. CreateDecryptor ();
Byt = Convert. FromBase64String (encryptedValue );
MS = new MemoryStream ();
Cs = new CryptoStream (MS, ct, CryptoStreamMode. Write );
Cs. Write (byt, 0, byt. Length );
Cs. FlushFinalBlock ();
Cs. Close ();
Return Encoding. UTF8.GetString (ms. ToArray ());
}
Public string DESDecrypt (string encryptedValue, string key)
{
Return DESDecrypt (encryptedValue, key, key );
}
Private string GetStringValue (byte [] Byte)
{
String tmpString = "";
If (this. isReturnNum = false)
{
ASCIIEncoding Asc = new ASCIIEncoding ();
TmpString = Asc. GetString (Byte );
}
Else
{
Int iCounter;
For (iCounter = 0; iCounter <Byte. Length; iCounter ++)
{
TmpString = tmpString + Byte [iCounter]. ToString ();
}
}
Return tmpString;
}
Private byte [] GetKeyByteArray (string strKey)
{
ASCIIEncoding Asc = new ASCIIEncoding ();
Int tmpStrLen = strKey. Length;
Byte [] tmpByte = new byte [tmpStrLen-1];
TmpByte = Asc. GetBytes (strKey );
Return tmpByte;
}
}
}
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.