C # encryption algorithm

Source: Internet
Author: User

 public Static class common    {        #region MD5 encryption        //&L T;summary>          //MD5 encryption          //&LT;/SUMMARY&G T          //<param name= "strsource" > Required encrypted string </param>     & nbsp    //&LT;RETURNS&GT;MD5 encrypted string </returns>           public static string Md5encrypt (String strsource)         {           //Put the string in a byte array  & nbsp;            byte[] Bytin = System.Text.Encoding.Default.GetBytes (strsource);  & nbsp        //Establish keys and offsets for encrypted objects                     &NB Sp Byte[] IV = {102, 16, 93, 156, 78, 4, 218, 32};//define offsets               byte[] Key = {55, 103, 246, 79, 36, 99, 167, 3};//define key              //Instance DES encryption class               DESCryptoServiceProvider mobjcryptoservice = new DESCryptoServiceProvider ();    &N Bsp       Mobjcryptoservice.key = iv;            MOBJCRYPTOSERVICE.IV = key;&nbsp ;           ICryptoTransform encrypto = Mobjcryptoservice.createencryptor ();      & nbsp    //instance MemoryStream stream encryption files               System.IO.MemoryStream ms = new System.IO.MemoryStream ();            CryptoStream cs = new CryptoStream (MS, Encrypto, Crypt Ostreammode.write);            CS. Write (bytin, 0, Bytin.length);            CS. FlushFinalBlock ();            return System.Convert.ToBase64String (Ms. ToArray ());      }       //<summary>          //MD5 Decrypt &N bsp;        //</summary>          //<param name= "Sou Rce "> String to Decrypt </param>          //&LT;RETURNS&GT;MD5 decrypted string </returns>           public static string Md5decrypt (String Source)         {           //Convert decrypted string to byte array               byte[] Bytin = Sy Stem. Convert.frombase64string (Source);           //gives decrypted key and offset, the key and offset must be the same as the key and offset at the time of encryption                Byte[] IV = {102, 16, 93, 156, 78, 4, 218, 32};//define offsets &NBSP;&NBSP;&NBSP ;           byte[] key = {55, 103, 246, 79, 36, 99, 167, 3};//define key       &NBS P       DESCryptoServiceProvider mobjcryptoservice = new DESCryptoServiceProvider ();            Mobjcryptoservice.key = iv;            MOBJCRYPTOSERVICE.IV = key;      &NBSP ;    //decryption of instance streams               System.IO.MemoryStream ms = new System.IO.Me Morystream (bytin, 0, Bytin.length);            ICryptoTransform encrypto = Mobjcryptoservic E.createdecryptor ();            CryptoStream cs = new CryptoStream (MS, Encrypto, Cryptostrea Mmode.read);            StreamReader strd = new StreamReader (cs, encoding.default);  &N Bsp         return STRD. ReadToEnd ();       }        #endregion         #region Dynamic Encryption &NB Sp       private static byte[] s_bytkey;        static Common ()   &nbsp     {            S_bytkey = new byte[] {0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0x16, 0x27, 0x38, 0x49, 0x61, 0x72, 0x83, 0x94, 0xa5, 0xb6};       }       //<sum mary>       //decryption        //</summary>       // <param name= "text" ></param>       //<param name= "key" ></param>  & nbsp    //<returns></returns>        public static string Putbykey (string text, St Ring key)         {            byte[] buffer = new Byte[text. length];            byte[] bytes = Encoding.UTF8.GetBytes ( Dscf.Global.Common.Common.DisposeId (key));            RijndaelManaged managed = new RijndaelManaged ();            buffer = CoNvert. FromBase64String (text);            MemoryStream stream = new MemoryStream ();    &N Bsp       CryptoStream stream2 = new CryptoStream (stream, managed. CreateDecryptor (bytes, s_bytkey), CryptoStreamMode.Write);            STREAM2. Write (buffer, 0, buffer. Length);            STREAM2. FlushFinalBlock ();            Encoding Encoding = new UTF8Encoding ();      &N Bsp     return encoding. GetString (stream. ToArray ());       }       //<summary>       ///Crypto & nbsp      //</summary>       //<param name= "text" ></param>  &N Bsp    //<param name= "key" ></param>       //<returns></returns>         public static string GetbYKey (string text, string key)         {            byte[] bytes = Encodin G.utf8. GetBytes (text);            byte[] RgbKey = Encoding.UTF8.GetBytes ( Dscf.Global.Common.Common.DisposeId (key));            RijndaelManaged managed = new RijndaelManaged ();            MemoryStream stream = new MemoryStream ();            CryptoStream stream2 = new CryptoStream (stream, managed. CreateEncryptor (RgbKey, S_bytkey), CryptoStreamMode.Write);            STREAM2. Write (bytes, 0, bytes. Length);            STREAM2. FlushFinalBlock ();            return convert.tobase64string (stream. ToArray ());       }       //<summary>       //Processing I D, convert to crypto key       //</summary>       //<param name= "id" ></param>       //<returns>& lt;/returns>        public static string Disposeid (String id)         {  &N Bsp         String strvalue = "";            for (int i = 0; I < 9-id. Length; i++)             {                strvalue + i;&nbsp ;          }            return ID. ToString () + strvalue;       }        #endregion    }

C # encryption algorithm

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.