Encryption of ASP--symmetric Encryption des algorithm (2)

Source: Internet
Author: User

  

And to the weekend, the afternoon blog to read two articles, about the old and old Zhao's programmer career , can not help but sigh over the long program road, Whither!

In an instant graduation of the third year, to Suzhou, ran Shanghai, from the beginning of the Lingyun ambition, last year, back from the luggage silently back to Changsha ready to buy a house, also want to have a home (after all grade is not small), have look forward work prospects.

See Old Zhao and old jump two elder stories, real and realistic, at the same time also have a lost lost small confused, dare to ask where the road!

  Sigh for a moment, life not only, code, continue into the pit!

Yesterday shared a bit of asymmetric encryption RSA algorithm , today to friends to share the symmetric encryption des algorithm case.

  Add decryption Process:

1, generate encryption keys key, the longer the password, the more difficult to crack, poor lifting too tired

2, encryption: By dividing, shifting, selecting and iterating to form a set of 16 encryption keys, respectively, for each round of operations to use

3. Decryption: The order of the decryption key is reversed

The above I understand des plus decryption process, of course, here involves password security, I involved superficial. Details Baidu "DES encryption Algorithm principle".

Most of the decryption algorithms in C # are concentrated in the System.Security.Cryptography space, keep in mind.

Here's a case to show you:

usingSystem;usingSystem.Globalization;usingSystem.IO;usingSystem.Security.Cryptography;usingSystem.Text;namespaceutils.password{/// <summary>    ///des plus decryption/// </summary>     Public Static classDES {/// <summary>        ///Generate key/// </summary>         Public Static voidGenerator ( out stringkey) {DESCryptoServiceProvider des=(DESCryptoServiceProvider) descryptoserviceprovider.create (); Key=ASCIIEncoding.ASCII.GetString (DES.        Key); }        /// <summary>        ///Encrypt/// </summary>        /// <param name= "password" ></param>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static stringDesencrypt (stringPasswordstringkey) {            byte[] data =Encoding.UTF8.GetBytes (password); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (key); Des.iv=ASCIIEncoding.ASCII.GetBytes (key); ICryptoTransform desencrypt=DES.            CreateEncryptor (); byte[] result = desencrypt.transformfinalblock (data,0, data.            Length); returnbitconverter.tostring (Result); }        /// <summary>        ///decryption/// </summary>        /// <param name= "password" ></param>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static stringDesdecrypt (stringPasswordstringkey) {            string[] sinput = password. Split ("-".            ToCharArray ()); byte[] data =New byte[Sinput.            Length];  for(inti =0; I < Sinput. Length; i++) {Data[i]=byte.            Parse (Sinput[i], numberstyles.hexnumber); } DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (key); Des.iv=ASCIIEncoding.ASCII.GetBytes (key); ICryptoTransform desencrypt=DES.            CreateDecryptor (); byte[] result = Desencrypt. TransformFinalBlock (data,0, data.            Length); returnEncoding.UTF8.GetString (Result); }        /// <summary>           ///How to encrypt data MD5/// </summary>           /// <param name= "str" ></param>           /// <param name= "key" >encryption Key</param>           /// <returns></returns>            Public Static stringDESEncryptMD5 (stringStrstringkey) {DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); byte[] inputbytearray; Inputbytearray=Encoding.Default.GetBytes (str); Des. Key= ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Key,"MD5"). Substring (0,8)); Des.iv= ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Key,"MD5"). Substring (0,8)); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des.            CreateEncryptor (), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs.            FlushFinalBlock (); StringBuilder ret=NewStringBuilder (); foreach(byteBinchMs. ToArray ()) Ret. AppendFormat ("{0:x2}", B); returnret.        ToString (); }        /// <summary>           ///How to decrypt data MD5/// </summary>           /// <param name= "str" ></param>           /// <param name= "key" >encryption Key</param>           /// <returns></returns>            Public Static stringDESDecryptMD5 (stringStrstringkey) {DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); intLen; Len= str. Length/2; byte[] Inputbytearray =New byte[Len]; intx, I;  for(x =0; x < Len; X + +) {i= Convert.ToInt32 (str. Substring (x *2,2), -); INPUTBYTEARRAY[X]= (byte) I; } des. Key= ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Key,"MD5"). Substring (0,8)); Des.iv= ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Key,"MD5"). Substring (0,8)); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des.            CreateDecryptor (), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs.            FlushFinalBlock (); returnEncoding.Default.GetString (Ms.        ToArray ()); }    } }

Invocation mode

 Public Static voiddes () {stringKey =""; Des. Generator ( outkey); varAAA = DES. DESEncryptMD5 ("123456789","12312312"); varBBB = DES. DESDecryptMD5 (AAA,"12312312"); varCC = DES. Desencrypt ("123456789", key); varDD =DES. Desdecrypt (cc, key);}

OK, you crossing, this period of the article symmetric encryption des algorithm to write here, thank you for your support, your support is my motivation!

The next issue to everyone is the encryption reversible and irreversible several ways, please look forward!!!

Encryption of ASP--symmetric Encryption des algorithm (2)

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.