Symmetric encryption, decryption, encryption and decryption
/// <Summary> /// encryption /// </summary> public static class Encrypting {# region uses symmetric encryption to decrypt private static string GetEncryptKey () {return ConfigurationManager. deleetask[ "EncryptKey"];} /// <summary> /// use symmetric encryption algorithms /// </summary> /// <param name = "str"> </param> /// <param name = "encryptKey"> </param> // <returns> </returns> public static string encryption ricencrypts (string str) {string result = string. empty; Byte [] inputData = System. text. encoding. UTF8.GetBytes (str); SymmetricAlgorithm Algorithm = null; MemoryStream msTarget = null; CryptoStream encStream = null; try {byte [] kv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; // if you need to specify an encryption algorithm, you can specify the parameters in the string // Create () method in the Create () parameter: DES, RC2 System, Rijndael, and TripleDES // different implementation classes have different requirements on the IV vector (you can use the GenerateIV () method Generated). If no parameter is specified, Rijndael Algorithm = equalricalgorithm is used. create (); // generate an encryption algorithm msTarget = new MemoryStream (); // defines the link of the data stream to the encrypted conversion stream. EncStream = new CryptoStream (msTarget, Algorithm. createEncryptor (Convert. fromBase64String (GetEncryptKey (), kv), CryptoStreamMode. write); encStream. write (inputData, 0, inputData. length); encStream. flushFinalBlock (); result = Convert. toBase64String (msTarget. toArray ();} catch (Exception) {return null;} finally {if (Algorithm! = Null) Algorithm. Clear (); if (msTarget! = Null) msTarget. Close (); if (encStream! = Null) encStream. close ();} return result ;} /// <summary> /// decrypt Using symmetric algorithms /// </summary> /// <param name = "encryptStr"> </param> /// <param name = "encryptKey"> </param> // <returns> </returns> public static string SymmectricDecrypts (string encryptStr) {string result = string. empty; SymmetricAlgorithm Algorithm = null; MemoryStream msTarget = null; CryptoStream decStream = null; // Convert is used for encryption. toBase6 4 String (), Convert must be used for decryption. fromBase64String () try {byte [] kv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; byte [] encryptData = Convert. fromBase64String (encryptStr); Algorithm = effecricalgorithm. create (); msTarget = new MemoryStream (); decStream = new CryptoStream (msTarget, Algorithm. createDecryptor (Convert. fromBase64String (GetEncryptKey ()), Kv), CryptoStreamMode. write); decStream. write (encryptData, 0, encryptData. length); decStream. flushFinalBlock (); result = System. text. encoding. default. getString (msTarget. toArray ();} catch (Exception) {return null;} finally {if (Algorithm! = Null) Algorithm. Clear (); if (msTarget! = Null) msTarget. Close (); if (decStream! = Null) decStream. Close ();} return result ;}# endregion}