Using system;using system.io;using system.data;using system.text;using system.diagnostics;using System.Security; Using system.security.cryptography;/* *. NET Framework, with its rich library support provided by the CLR, requires little code to implement cryptographic algorithms that were previously difficult to implement in legacy languages such as C. This class implements some common secret algorithms, for reference. Where the MD5 algorithm returns the ToString string of int. Algorithm for returning numeric alphabetic results see previous blog post */namespace Archives digitization processing {//<summary>////Class Name: Hashencrypt///function: Hashes the passed-in string and returns the The H algorithm encrypts the string. Property: [None]///Structure Letter Amount Parameter:///Isreturnnum: Whether to return the byte code for the post-encryption character///iscasesensitive: Case-sensitive. Methods: This class provides four kinds of algorithms such as md5,sha1,sha256,sha512, and the length of the encryption string increases successively. </summary> public class Hashencrypt {//private string strin; private bool Isreturnnum; private bool iscasesensitive; <summary>///class initialization, this class provides four kinds of algorithms such as md5,sha1,sha256,sha512, the length of the encryption string increases in turn. </summary>//<param name= "iscasesensitive" > Case sensitivity </param>//<param name= "Isret Urnnum "> Whether to return the byte code for the post-encryption 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; The 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); The 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); The 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); The 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" > Strings to be encrypted </param>//<param name= "key" > key (maximum length 8) </param>//<pa Ram Name="IV" > Initialization vector (maximum length 8) </param>//<returns> Encrypted string </returns> public string Desencrypt (Strin G OriginalValue, String key, String IV) {//The key and IV are processed 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 stringDesencrypt (String originalvalue, string key) {return desencrypt (OriginalValue, key, key); }///<summary>//Use des decryption (Added by Niehl 2005-4-6)///</summary>//<param Name= "Encryptedvalue" > String to Decrypt </param>//<param name= "key" > key (maximum length 8) </param>//<p Aram Name= "IV" >m initialization vector (maximum length 8) </param>///<returns> decrypted string </returns> public string desde Crypt (string encryptedvalue, String key, String IV) {//The key and IV are processed 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; } }}
MD5,SHA1,SHA256,SHA512 and other common encryption algorithms