Des encryption Depth Optimization--random generation of encrypted strings

Source: Internet
Author: User

Des is known as the Data Encryption Standard, which is an encryption-based block algorithm using key encryption. However, once the key is fixed, the encrypted string is fixed, which is not conducive to the security of data storage. And with this method of encryption there is a significant mark, the last few are ' = '. All this optimization has some optimizations for keys and de-etc.

First we generate a 8-bit random string as the encrypted key value.

  Private Static stringso ="1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"; /// <summary>        ///generate content Random key/// </summary>        /// <returns></returns>         Public Static stringgetcontent () {Random rand=NewRandom (); stringstr =NULL;  for(inti =0; I <8; i++) {str+ = So. Substring (Rand. Next ( +),1); }            returnstr; }

The key value is then split into two parts.

   New Random ();                 int r = rand. Next (9);                 string Encryptkey = getcontent ();                 string first = encryptkey.substring (0, R);                 string last = encryptkey.substring (r);

Finally, the string to be encrypted DES encryption, and record the number of equals and intercept the removal of the equals string, and then with the key value of the left and right part of the stitching.

    Private Static byte[] Keys = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF }; /// <summary>         ///des encrypted String/// </summary>         /// <param name= "encryptstring" >string to encrypt</param>         /// <returns>encryption successfully returns the encrypted string, failed to return the source string</returns>          Public Static stringEncrypt (stringencryptstring) {            Try{Random Rand=NewRandom (); intr = Rand. Next (9); stringEncryptkey =getcontent (); stringFirst = Encryptkey.substring (0, R); stringLast =encryptkey.substring (R); byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0,8)); byte[] Rgbiv =Keys; byte[] Inputbytearray =Encoding.UTF8.GetBytes (encryptstring); DESCryptoServiceProvider DCSP=NewDESCryptoServiceProvider (); MemoryStream Mstream=NewMemoryStream (); CryptoStream Cstream=NewCryptoStream (Mstream, Dcsp.createencryptor (RgbKey, Rgbiv), cryptostreammode.write); Cstream.write (Inputbytearray,0, inputbytearray.length);                Cstream.flushfinalblock (); stringstr =convert.tobase64string (Mstream.toarray ()); inti =0;  for(intj = Str. Length-1; J >0; j--)                {                    if(Str[j]! ='=')                    {                         Break; }                    Else{i++; }} str= str. Substring (0Str. Length-i); returni.ToString () +r.tostring () +first + str +Last ; }            Catch            {                returnencryptstring; }        }

Decrypts the encrypted string.

    /// <summary>         ///des decryption String/// </summary>         /// <param name= "decryptstring" >string to decrypt</param>         /// <returns>decryption succeeded in returning the decrypted string, failed to return the source string</returns>          Public Static stringDecrypt (stringdecryptstring) {            Try            {                intCode = Int32.Parse (Decryptstring.substring (1,1)); inti = Int32.Parse (decryptstring.substring (0,1)); stringFirst = Decryptstring.substring (2, code); stringLast = decryptstring.substring (Decryptstring.length-8+code); stringEncryptkey =first+Last ; Decryptstring= decryptstring.substring (code +2, Decryptstring.length-Ten);  for(intj =0; J < I; J + +) {decryptstring+="="; }                byte[] RgbKey =Encoding.UTF8.GetBytes (Encryptkey); byte[] Rgbiv =Keys; byte[] Inputbytearray =convert.frombase64string (decryptstring); DESCryptoServiceProvider DCSP=NewDESCryptoServiceProvider (); MemoryStream Mstream=NewMemoryStream (); CryptoStream Cstream=NewCryptoStream (Mstream, DCSP.                CreateDecryptor (RgbKey, Rgbiv), cryptostreammode.write); Cstream.write (Inputbytearray,0, inputbytearray.length);                Cstream.flushfinalblock (); returnEncoding.UTF8.GetString (Mstream.toarray ()); }            Catch            {                returndecryptstring; }        }

Applications and demos:

    Public Static voidMain (string[] args) {            stringstr ="Xuhang";  for(inti =0; I <Ten; i++)            {                varm =Desnew.encrypt (str);                Console.WriteLine (m);                Console.WriteLine (Desnew.decrypt (m)); Console.WriteLine ("------------------------------"); Thread.Sleep ( -);//wake-up thread after 0.5s to avoid consistent data generation due to short time            }        }

Des encryption Depth optimization--randomly generate an encrypted string

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.