ASP. NET encryption and decryption algorithm sharing, asp.net encryption and decryption algorithm

Source: Internet
Author: User
Tags hmac key string pkcs7

ASP. NET encryption and decryption algorithm sharing, asp.net encryption and decryption algorithm

# Region DES encryption and decryption // <summary> // DES encryption // </summary> // <param name = "strSource"> string to be encrypted </param >/// <param name = "key"> 32-bit Key value </param> /// <returns> encrypted string </returns> public string DESEncrypt (string strSource) {return DESEncrypt (strSource, cipher ey);} public string DESEncrypt (string strSource, byte [] key) {fig sa = Rijndael. create (); sa. key = key; sa. mode = CipherMode. ECB; sa. padding = PaddingMode. zeros; MemoryStream MS = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, sa. createEncryptor (), CryptoStreamMode. write); byte [] byt = Encoding. unicode. getBytes (strSource); cs. write (byt, 0, byt. length); cs. flushFinalBlock (); cs. close (); return Convert. toBase64String (ms. toArray ());} /// <summary> // DES decryption // </summary> /// <param name = "strSource"> string to be decrypted </param> /// <param Name = "key"> 32-bit Key value </param> // <returns> decrypted string </returns> public string DESDecrypt (string strSource) {return DESDecrypt (strSource, cipher ey);} public string DESDecrypt (string strSource, byte [] key) {fig sa = Rijndael. create (); sa. key = key; sa. mode = CipherMode. ECB; sa. padding = PaddingMode. zeros; ICryptoTransform ct = sa. createDecryptor (); byte [] byt = Convert. fromBase64String (st RSource); MemoryStream MS = new MemoryStream (byt); CryptoStream cs = new CryptoStream (MS, ct, CryptoStreamMode. read); StreamReader sr = new StreamReader (cs, Encoding. unicode); return sr. readToEnd ();} # endregion # region a hash-based encryption and decryption method // <summary> /// encryption /// </summary> /// <param name = "src"> </param> // <returns> </returns> public static string EncryptStrByHash (string src) {if (src. length = 0) {Return "";} byte [] HaKey = System. text. encoding. ASCII. getBytes (src + "Test "). toCharArray (); byte [] HaData = new byte [20]; HMACSHA1 Hmac = new HMACSHA1 (HaKey); CryptoStream cs = new CryptoStream (Stream. null, Hmac, and CryptoStreamMode. write); try {cs. write (HaData, 0, HaData. length);} finally {cs. close ();} string HaResult = System. convert. toBase64String (Hmac. hash ). substring (0, 16); byte [] RiKe Y = System. text. encoding. ASCII. getBytes (HaResult. toCharArray (); byte [] RiDataBuf = System. text. encoding. ASCII. getBytes (src. toCharArray (); byte [] EncodedBytes = {}; MemoryStream MS = new MemoryStream (); RijndaelManaged rv = new RijndaelManaged (); cs = new CryptoStream (MS, rv. createEncryptor (RiKey, RiKey), CryptoStreamMode. write); try {cs. write (RiDataBuf, 0, RiDataBuf. length); cs. flushFinalBlock (); EncodedBytes = ms. toArray ();} finally {ms. close (); cs. close ();} return HaResult + System. convert. toBase64String (EncodedBytes );} /// <summary> /// decrypt /// </summary> /// <param name = "src"> </param> /// <returns> </ returns> public static string DecrypStrByHash (string src) {if (src. length <40) return ""; byte [] SrcBytes = System. convert. fromBase64String (src. substring (16); byte [] RiKey = Syste M. text. encoding. ASCII. getBytes (src. substring (0, 16 ). toCharArray (); byte [] InitialText = new byte [SrcBytes. length]; RijndaelManaged rv = new RijndaelManaged (); MemoryStream MS = new MemoryStream (SrcBytes); CryptoStream cs = new CryptoStream (MS, rv. createDecryptor (RiKey, RiKey), CryptoStreamMode. read); try {cs. read (InitialText, 0, InitialText. length);} finally {ms. close (); cs. close ();} System. T Ext. stringBuilder Result = new System. text. stringBuilder (); for (int I = 0; I <InitialText. length; ++ I) if (InitialText [I]> 0) Result. append (char) InitialText [I]); return Result. toString () ;}/// <summary> // recode the encrypted ciphertext. If the ciphertext length is greater than 16, remove the first 16 characters. If the ciphertext length is less than 16, returns an empty string /// </summary> /// <param name = "s"> </param> /// <returns> </returns> public string ReEncryptStrByHash (string s) {string e = Encrypt. encryptSt RByHash (s); return (e. Length> 16 )? E. substring (16): "") ;}# endregion # region Md5 encryption, generate 16-bit or 32-bit, the generated ciphertext is capitalized public static string Md5To16 (string str) {MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider (); string t2 = BitConverter. toString (md5.ComputeHash (UTF8Encoding. default. getBytes (str), 4, 8); t2 = t2.Replace ("-", ""); return t2 ;} //// <summary> /// MD5 32-bit encryption /// </summary> /// <param name = "str"> </param> // <returns> </returns> public static string Md5To32 (string str) {string pwd = ""; MD5 md5 = MD5.Create (); byte [] s = md5.ComputeHash (Encoding. UTF8.GetBytes (str); for (int I = 0; I <s. length; I ++) {pwd = pwd + s [I]. toString ("X");} return pwd;} # endregion # region 3DES encryption and decryption public string Encrypt3DES (string str) {// key string sKey = "wyw308 "; //// vector, can be empty string sIV = "scf521"; // construct symmetric algorithm SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider (); ICryptoTransform ct; MemoryStream MS; CryptoStream cs; byte [] byt; mCSP. key = Convert. fromBase64String (sKey); mCSP. IV = Convert. fromBase64String (sIV); mCSP. mode = System. security. cryptography. cipherMode. ECB; mCSP. padding = System. security. cryptography. paddingMode. PKCS7; ct = mCSP. createEncryptor (mCSP. key, mCSP. IV); byt = Encoding. UTF8.GetBytes (str); 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 ());} /// <summary> /// 3DES encryption with the specified key and vector /// </summary> /// <param name = "str"> </param>/ // <param name = "sKey"> </param> // <param name = "sIV"> </param> // <returns> </returns> public string Encrypt3DES (string str, string sKey, string sIV) {javasricalgorithm mCSP = new TripleDESCryptoServiceProvider (); ICryptoTransform ct; MemoryStream MS; CryptoStream cs; byte [] byt; mCSP. key = Convert. fromBase64String (sKey); mCSP. IV = Convert. fromBase64String (sIV); mCSP. mode = System. security. cryptography. cipherMode. ECB; mCSP. padding = System. security. cryptography. paddingMode. PKCS7; ct = mCSP. createEncryptor (mCSP. key, mCSP. IV); byt = Encoding. UTF8.GetBytes (str); 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 ();} // decrypt public string Decrypt3DES (string Value) {string sKey = "wyw308"; string sIV = "scf521"; incluricalgorithm mCSP = new TripleDESCryptoServiceProvider (); ICryptoTransform ct; MemoryStream MS; CryptoStream cs; byte [] byt; mCSP. key = Convert. fromBase64String (sKey); mCSP. IV = Convert. fromBase64String (sIV); mCSP. mode = System. security. cryptography. cipherMode. ECB; mCSP. padding = System. security. cryptography. paddingMode. PKCS7; ct = mCSP. createDecryptor (mCSP. key, mCSP. IV); byt = Convert. fromBase64String (Value); 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 ());} /// <summary> /// 3 DES decryption with the specified key and vector /// </summary> /// <param name = "Value"> </param>/ // <param name = "sKey"> </param> // <param name = "sIV"> </param> // <returns> </returns> public string Decrypt3DES (string str, string sKey, string sIV) {javasricalgorithm mCSP = new TripleDESCryptoServiceProvider (); ICryptoTransform ct; MemoryStream MS; CryptoStream cs; byte [] byt; mCSP. key = Convert. fromBase64String (sKey); mCSP. IV = Convert. fromBase64String (sIV); mCSP. mode = System. security. cryptography. cipherMode. ECB; mCSP. padding = System. security. cryptography. paddingMode. PKCS7; ct = mCSP. createDecryptor (mCSP. key, mCSP. IV); byt = Convert. fromBase64String (str); 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 () ;}# endregion # region a simple encryption and decryption method. Only public static string EnCryptEnStr (string str) English is supported) // Add 1 encryption in reverse order {byte [] by = new byte [str. length]; for (int I = 0; I <= str. length-1; I ++) {by [I] = (byte) str [I] + 1) ;}str = ""; for (int I =. length-1; I> = 0; I --) {str + = (char) by [I]). toString ();} return str;} public static string DeCryptEnStr (string str) // decode {byte [] by = new byte [str. length]; for (int I = 0; I <= str. length-1; I ++) {by [I] = (byte) str [I]-1) ;}str = ""; for (int I =. length-1; I> = 0; I --) {str + = (char) by [I]). toString () ;}return str ;}# endregion # region a simple encryption and decryption method. The Chinese public static string EnCryptCnStr (string str) is supported on the basis of the previous one) {string htext = ""; // blank text for (int I = 0; I <str. length; I ++) {htext = htext + (char) (str [I] + 10-1*2);} return htext;} public static string DeCryptCnStr (string str) {string dtext = ""; for (int I = 0; I <str. length; I ++) {dtext = dtext + (char) (str [I]-10 + 1*2);} return dtext ;} # endregion # region Url address encoding and decoding // <summary> // encode the Url address /// </summary> /// <param name = "url"> </ param> // <returns> </returns> public static string UrlEncode (string url) {byte [] mByte = null; mByte = System. text. encoding. getEncoding ("GB2312 "). getBytes (url); return System. web. httpUtility. urlEncode (mByte );} /// <summary> /// decodes the Url // </summary> /// <param name = "url"> </param> /// <returns> </returns> public static string UrlDecode (string url) {return HttpUtility. urlDecode (url, System. text. encoding. getEncoding ("GB2312");} # endregion

The above is all the content of this article. I hope you will like it.

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.