Asp. NET encryption and decryption algorithm sharing _ practical skills

Source: Internet
Author: User
#region des encryption and decryption///<summary>///des encryption///</summary>///<param name= "strsource" > Pending encrypted Word Strings </param>///<param name= "key" >32 bit key value </param>///<returns> encrypted string </returns> p
    Ublic string Desencrypt (String strsource) {return desencrypt (strsource, Deskey);
      public string Desencrypt (String strsource, byte[] key) {symmetricalgorithm 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" > Strings to be decrypted </param&gt
;    <param name= "key" >32 bit key value </param>///<returns> decrypted string </returns> public string DES
    Decrypt (String strsource) {return desdecrypt (strsource, Deskey);
      public string Desdecrypt (String strsource, byte[] key) {symmetricalgorithm sa = rijndael.create (); Sa.
      key = key; Sa.
      Mode = CIPHERMODE.ECB; Sa.
      Padding = Paddingmode.zeros; ICryptoTransform ct = sa.
      CreateDecryptor ();
      byte[] byt = convert.frombase64string (strsource);
      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-implemented cryptographic decryption method///<summary>///encryption///</summary>///<para M name= "src" ></param>///<returns></returns> public static string Encryptstrbyhash (String sr c) {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, CryptoStreamMode.Write); try {cs.
      Write (hadata, 0, hadata.length); finally {cs.
      Close (); String haresult = System.Convert.ToBase64String (Hmac.hash).
      Substring (0, 16);
      byte[] Rikey = 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>///decryption///</summary>///<param name= "src" ></param>///<r eturns></returns> public static string Decrypstrbyhash (String src) {if (src).
      Length <) return ""; byte[] srcbytes = System.Convert.FromBase64String (src.
      Substring (16)); byte[] Rikey = System.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.Text.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>///to encode encrypted ciphertext, if the cipher is long >16, remove the first 16 characters, if the length is less than 16, return an empty string///</summary>///<p
    Aram Name= "s" ></param>///<returns></returns> public string Reencryptstrbyhash (string s)
      {String E = Encrypt.encryptstrbyhash (s);
    Return ((E.length >) e.substring (16): ""); #endregion #region MD5 Encryption, generates 16-bit or 32-bit, generated ciphertext is all uppercase public static string md5to16 (String str) {Md5cryptos
      Erviceprovider 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) {s
      Tring 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 Decryption public string encrypt3des (String str) {//key string SKey = "Wyw3
      08 ";
      Vector, can be empty string SIV = "scf521";
      Constructing 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///</summary>///<param name= "str" with specified keys and vectors ></param> <param name= "SKey" ></param>///<param name= "SIV" ></param>///<returns>< /returns> public string Encrypt3des (String str, string SKey, String SIV) {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 ());
      //Decrypt public string Decrypt3des (string Value) {string sKey = "wyw308";
      String SIV = "scf521";
      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.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>///3DES decryption///</summary>///<param name= "Value" with specified keys and vectors ></param&gt
    ; <param name= "SKey" ></param>///<param name= "SIV" ></param>///<returns></ret Urns> public string Decrypt3des (String str, string SKey, String sivs) {SymmetricAlgorithm MCSP = new Trip
      Ledescryptoserviceprovider ();
      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 decryption method that only supports English public static string Encryptenstr (String str)//Reverse plus 1 encryption {byte [] by = new BYTE[STR.
      Length];
       for (int i = 0; I <= str.
       Length-1;
      i++) {By[i] = (byte) ((byte) str[i] + 1);
      } str = ""; for (int i = by.
       Length-1;
       I >= 0; i--) {str = ((char) by[i]).
      ToString ();
    return str; public static string Decryptenstr (String str)//order minus 1 decoding {byte[] by = new BYTE[STR.
      Length];
       for (int i = 0; I <= str.
       Length-1;
      i++) {By[i] = (byte) ((byte) str[i]-1);
      } str = ""; for (int i = by.
       Length-1;
       I >= 0; i--) {str = ((char) by[i]).
      ToString ();
return str;    #endregion #region A simple encryption decryption method that supports the Chinese public static string Encryptcnstr (String str) {St on the previous basis Ring 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 codec///<summary>///encoded URL///</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>///decoding URL address///</summary>///<paramName= "url" ></param>///<returns></returns> public static string UrlDecode (string url) {
    Return Httputility.urldecode (URL, System.Text.Encoding.GetEncoding ("GB2312"));
 } #endregion

The above is the entire contents of this article, I hope you can enjoy.

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.