C # Encryption Class,

Source: Internet
Author: User

C # Encryption Class,

Var es = EncryptSugar. getInstance (); string word = "abc"; var wordEncrypt = es. encrypto (word); // encrypt var wordDecrypt = es. decrypto (wordEncrypt); // decrypt var wordMd5 = es. MD5 (word); // md5 Encryption

  

 

Using System; using System. collections. generic; using System. text; using System. security. cryptography; using System. xml; using System. IO; using System. linq; namespace SyntacticSugar {// <summary> /// ** Description: Encryption Class // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan // ** usage instructions: http://www.cnblogs.com/sunkaixuan/p/4610729.html /// </Summary> public class EncryptSugar {private static readonly object _ instanceLock = new object (); private static EncryptSugar _ instance = null; private static encryption ricalgorithm mobjCryptoService; private static string _ key; private static readonly object _ cacheLock = new object (); private static Dictionary <string, string> _ cache = new Dictionary <string, string> (); /// <summary> /// maximum number of cached items // /</Summary> private static int _ maxCacheNum = 10000; /// <summary> /// constructor of the symmetric encryption class /// </summary> public static EncryptSugar GetInstance () {if (_ instance = null) {lock (_ instanceLock) {if (_ instance = null) {mobjCryptoService = new RijndaelManaged (); _ key = "Guz (% & as1213 ^ d (fa % (HilJ $ lhj! Y6 & (* jkP87jH7 "; _ instance = new EncryptSugar () ;}} return _ instance ;} /// <summary> /// encryption method /// </summary> /// <param name = "Source"> string to be encrypted </param> /// <returns> encrypted string </returns> public string Encrypto (string source) {if (_ cache. containsKey (source) {return _ cache [source];} byte [] bytIn = UTF8Encoding. UTF8.GetBytes (source); MemoryStream MS = new MemoryStream (); mobjCryptoService. key = GetLegalKey (); mobjCryptoService. IV = GetLegalIV (); ICryptoTransform encrypto = mobjCryptoService. createEncryptor (); CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. write); cs. write (bytIn, 0, bytIn. length); cs. flushFinalBlock (); ms. close (); byte [] bytOut = ms. toArray (); string reval = Convert. toBase64String (bytOut); lock (_ cacheLock) {if (_ cache. count> _ maxCacheNum) {foreach (var it in _ cache. take (_ maxCacheNum/5) {_ cache. remove (it. key) ;}}_ cache. add (source, reval);} return reval ;;} /// <summary> /// Decryption Method /// </summary> /// <param name = "Source"> string to be decrypted </param> /// <returns> decrypted string </returns> public string Decrypto (string source) {lock (_ cacheLock) {if (_ cache. any (it => it. value = source) {return _ cache. single (it => it. value = source ). key ;}} byte [] bytIn = Convert. fromBase64String (source); MemoryStream MS = new MemoryStream (bytIn, 0, bytIn. length); mobjCryptoService. key = GetLegalKey (); mobjCryptoService. IV = GetLegalIV (); ICryptoTransform encrypto = mobjCryptoService. createDecryptor (); CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. read); StreamReader sr = new StreamReader (cs); return sr. readToEnd () ;}/// <summary> // MD5 encryption, irreversible // </summary> /// <param name = "source"> </param> /// <returns> </returns> public string MD5 (string source) {return System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (source, "MD5 ");} # region private function // <summary> // obtain the key // </summary> // <returns> key </returns> private byte [] GetLegalKey () {string sTemp = _ key; mobjCryptoService. generateKey (); byte [] bytTemp = mobjCryptoService. key; int KeyLength = bytTemp. length; if (sTemp. length> KeyLength) sTemp = sTemp. substring (0, KeyLength); else if (sTemp. length <KeyLength) sTemp = sTemp. padRight (KeyLength, ''); return ASCIIEncoding. ASCII. getBytes (sTemp );} /// <summary> /// obtain the initial vector IV /// </summary> /// <returns> initial test vector IV </returns> private byte [] GetLegalIV () {string sTemp = "asdfas & dfg * $ # +) * Y41sdgsdgs & * % $ ^ & GGslsadKdfK1"; mobjCryptoService. generateIV (); byte [] bytTemp = mobjCryptoService. IV; int IVLength = bytTemp. length; if (sTemp. length> IVLength) sTemp = sTemp. substring (0, IVLength); else if (sTemp. length <IVLength) sTemp = sTemp. padRight (IVLength, ''); return ASCIIEncoding. ASCII. getBytes (sTemp) ;}# endregion }}

  

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.