C # Urlengine (encryption/decryption)

Source: Internet
Author: User

URL is a class DES encryption/decryption algorithm UrlEncode is mainly the not ASC characters and special characters

with%x/hex returns a new string after processing the link,

        public static string UrlEncode (String uristring, Encoding uriencoding)        {            StringBuilder str = new StringBuilder () ;            byte[] buf = uriencoding.getbytes (uristring);            for (int i = 0; i < BUF. Length; i++)                str. Append ("%" + convert.tostring (Buf[i], +));            return convert.tostring (str);        }

The above code is a way many people use in C #, but it's not a standard UrlEncode

So let's see what it does. The result of encryption differs from another encryption,

Updated Definitions/Wow

The code above:%d2%d1%b8%fc%d0%c2%b5%c4%b6%a8%d2%e5%20%2f%20%57%6f%77

Another type of code:%D2%D1%B8%FC%D0%C2%B5%C4%B6%A8%D2%E5%20/%20WOW

Very simple when you search for a term in Baidu or Google, you can try the above entry you will find

The encrypted result will not be the result of the above code encryption, although it will not be too much problem, but the code

Encryption is not a standard can not be hidden, it should not be converted to the white let the computer in the decryption to do more operations,

Now let's look at the correct URL encryption code

        [DllImport ("kernel32.dll", SetLastError = true)] Private unsafe static extern sbyte* Sethandlecount (byte[]        Value);            private unsafe static int strlen (sbyte* ptr) {int i = 0;            while (* (ptr++)! = 0) i++;        return i; } private unsafe static string UrlEncode (String uristring, Encoding uriencoding) {string eax, St R = String.            Empty;            sbyte* ptr = Sethandlecount (uriencoding.getbytes (uristring)); for (int i = 0, len = strlen (PTR), i < Len; i++, ptr++) {int asc = *ptr < 0? n + *ptr                : *ptr; if (ASC < | | asc = = | | | ASC > $ && ASC < | | | ASC > && ASC <                    sc > 122) {eax = convert.tostring (ASC, 16); str + = eax. Length < 2?                "%0" + eax: "%" + eax;     } else str + = (char) ASC;       } return str; }

Don't you feel a lot more complicated but it's a standard URL encryption method

By multiple languages take similar, encrypt the same some URL decryption code is not supported

A string that is not standard URL is encrypted, although the odds are small but it does not mean that there is no

Above the encryption everyone can also be optimized, I just don't want to define byte[]

variables, declaring variables too much I hate it.

When using the code above, you will need to tick the project properties to allow unsafe code

Otherwise you cannot compile to debug because the code contains the local code section

public static string UrlDecode (String uristring, Encoding Encoding)        {            ArrayList str = new ArrayList ();            for (int i = 0; i < uristring.length; i++)            {                if (uristring[i] = = '% ')                {                    string eax = Uristring.substrin G (++i, 2);                    Str. ADD (Convert.tobyte (eax, +));  i++;                }                Else                    str. ADD (Convert.tobyte (uristring[i));            }            return encoding. GetString ((byte[]) str. ToArray (typeof (Byte)));        }

Above is the URL decryption method section, which can support non-standard URL encryption after

The core of the string is mainly based on the URL encoding when the not ASC character and

special characters from hex swap bytes are deposited into the BUF and eventually return to the text ,



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Urlengine (encryption/decryption)

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.