Symmetric encryption, decryption, encryption and decryption

Source: Internet
Author: User

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}

 

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.