[Post] encode and decode JavaScript base64 to implement URL parameter transfer.

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 of mybase64
///
Public class mybase64
{
Public mybase64 ()
{
//
// Todo: add the constructor logic here
//
}
///
/// Base64 encoding on the server
///
///


///
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 );
}
}
///
/// Base64 decoding on the server
///
///


///
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 );

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.