Encode and decode JavaScript Base64 to transmit URL parameters.

Source: Internet
Author: User
Tags 0xc0

Why do we need to encode parameters? I believe that the majority of programmers who have experience in development know that in the Web, if the parameter value is directly transferred on the Url address, if it is Chinese, or +, there will be garbled characters, if there is no problem with numbers or English words, simply put, the passed parameters need to be encoded.
Here, some people may say, why not use Server. UrlDecode and Server. UrlEncode for encoding and decoding?

Indeed, these two server-side objects are very useful and easy to use. However, if the client is HTML Input, the page is HTML or other during query, but it is not. NET, can this object be used?


Now I have encountered such a problem: the query stuff is placed on the page, and that page I don't want him to be. at the end of aspx, Haha, I feel that HTML is quite good, and the controls in it are also HTML objects.

Let's take a look at two functions: UTF16 to UTF8 and UTF8 to Utf16.
Function utf16to8 (str ){
Var out, I, len, c;

Out = "";
Len = str. length;
For (I = 0; I <len; I ++ ){
C = str. charCodeAt (I );
If (c> = 0x0001) & (c <= 0x007F )){
Out + = str. charAt (I );
} Else if (c> 0x07FF ){
Out + = String. fromCharCode (0xE0 | (c> 12) & 0x0F ));
Out + = String. fromCharCode (0x80 | (c> 6) & 0x3F ));
Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F ));
} Else {
Out + = String. fromCharCode (0xC0 | (c> 6) & 0x1F ));
Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F ));
}
}
Return out;
}

Function utf8to16 (str ){
Var out, I, len, c;
Var char2, char3;

Out = "";
Len = str. length;
I = 0;
While (I <len ){
C = str. charCodeAt (I ++ );
Switch (c> 4)
{
Case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0 xxxxxxx
Out + = str. charAt (I-1 );
Break;
Case 12: case 13:
// 110x xxxx 10xx xxxx
Char2 = str. charCodeAt (I ++ );
Out + = String. fromCharCode (c & 0x1F) <6) | (char2 & 0x3F ));
Break;
Case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
Char2 = str. charCodeAt (I ++ );
Char3 = str. charCodeAt (I ++ );
Out + = String. fromCharCode (c & 0x0F) <12) |
(Char2 & 0x3F) <6) |
(Char3 & 0x3F) <0 ));
Break;
}
}

Return out;
}

So why is conversion required? Because the Chinese characters obtained in JavaScript are encoded with UTF16, and our unified page standard format UTF-8 can be different oh, so the need for first conversion, the above function UTF-16 to UTF8, and then encode it with base64.

The following are operations related to Base64 encoding and decoding in javascript:

Var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /";
Var base64DecodeChars = new Array (
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-62,-1, -1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1 );
// Client Base64 encoding
Function base64encode (str ){
Var out, I, len;
Var c1, c2, c3;

Len = str. length;
I = 0;
Out = "";
While (I <len ){
C1 = str. charCodeAt (I ++) & 0xff;
If (I = len)
{
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4 );
Out + = "= ";
Break;
}
C2 = str. charCodeAt (I ++ );
If (I = len)
{
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4) | (c2 & 0xF0)> 4 ));
Out + = base64EncodeChars. charAt (c2 & 0xF) <2 );
Out + = "= ";
Break;
}
C3 = str. charCodeAt (I ++ );
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4) | (c2 & 0xF0)> 4 ));
Out + = base64EncodeChars. charAt (c2 & 0xF) <2) | (c3 & 0xC0)> 6 ));
Out + = base64EncodeChars. charAt (c3 & 0x3F );
}
Return out;
}
// Client Base64 Decoding
Function base64decode (str ){
Var c1, c2, c3, c4;
Var I, len, out;

Len = str. length;
I = 0;
Out = "";
While (I <len ){
/* C1 */
Do {
C1 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I <len & c1 =-1 );
If (c1 =-1)
Break;

/* C2 */
Do {
C2 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I <len & c2 =-1 );
If (c2 =-1)
Break;

Out + = String. fromCharCode (c1 <2) | (c2 & 0x30)> 4 ));

/* C3 */
Do {
C3 = str. charCodeAt (I ++) & 0xff;
If (c3 = 61)
Return out;
C3 = base64DecodeChars [c3];
} While (I <len & c3 =-1 );
If (c3 =-1)
Break;

Out + = String. fromCharCode (c2 & 0XF) <4) | (c3 & 0x3C)> 2 ));

/* C4 */
Do {
C4 = str. charCodeAt (I ++) & 0xff;
If (c4 = 61)
Return out;
C4 = base64DecodeChars [c4];
} While (I <len & c4 =-1 );
If (c4 =-1)
Break;
Out + = String. fromCharCode (c3 & 0x03) <6) | c4 );
}
Return out;
}

In this way, the previous values can be decoded on the server.
The following are the classes related to C # Base64 encoding and decoding:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;

Namespace CNVP. Base64
{
/// <Summary>
/// Summary of MyBase64
/// </Summary>
Public class MyBase64
{
Public MyBase64 ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Base64 encoding on the server
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public string base64Encode (string data)
{
Try
{
Byte [] encData_byte = new byte [data. Length];
EncData_byte = System. Text. Encoding. UTF8.GetBytes (data );
String encodedData = Convert. ToBase64String (encData_byte );
Return encodedData;
}
Catch (Exception e)
{
Throw new Exception ("Error in base64Encode" + e. Message );
}
}
/// <Summary>
/// Base64 decoding on the server
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public string base64Decode (string data)
{
Try
{
System. Text. UTF8Encoding encoder = new System. Text. UTF8Encoding ();
System. Text. Decoder utf8Decode = encoder. GetDecoder ();
Byte [] todecode_byte = Convert. FromBase64String (data );
Int charCount = utf8Decode. GetCharCount (todecode_byte, 0, todecode_byte.Length );
Char [] decoded_char = new char [charCount];
Utf8Decode. GetChars (todecode_byte, 0, todecode_byte.Length, decoded_char, 0 );
String result = new String (decoded_char );
Return result;
}
Catch (Exception e)
{
Throw new Exception ("Error in base64Decode" + e. Message );
}
}
}
}


Var Keyword = base64encode (utf16to8 (document. all. Keyword. value ));
Keyword = Keyword. replace ("+", "% 2B"); // replace +; otherwise, an error occurs during server decoding.

Run the following code on the server:
CNVP. Base64.MyBase64 base64 = new CNVP. Base64.MyBase64 ();
Keyword = base64.base64Decode (Keyword );

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.