JavaScript BASE64 encoding and decoding to implement URL parameter passing. _javascript Tips

Source: Internet
Author: User
Tags 0xc0 base64

Why do I need to encode a parameter? I believe that the vast number of programmers who have developed experience know that in the web, if directly in the URL address to pass the parameter value, if Chinese, or + and so on will appear garbled phenomenon, if the number or English as if there is no problem, in short, passed over the parameters are needed to encode.
Here, some might say, why not encode and decode directly with Server.urldecode and Server.URLEncode?

Indeed, the two server-side objects are well used and convenient to use, but if the client is HTML input, the page is HTML or otherwise, not anyway. NET, can this object still be used?


I have this problem now, the query is on the page, and the page I don't want him to be. aspx end, ha, feel the HTML is pretty good, and the inside of the control is also used HTML object.

!--more--> to see 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);
&NBSP} else if (C > 0x07ff) {
     out = = String.fromCharCode (0xe0 | ((c >>) & 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:
0xxxxxxx
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 does it need to be transformed? Because the Chinese characters in JavaScript are encoded using UTF16, and our Unified page standard format UTF-8 is not the same oh, so need to convert first, the above function UTF-16 to UTF8, and then Base64 coding.

The following is about JS for Base64 encoding and decoding related operations:

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,,-62, 1,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-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;
}

This passes past values to decode the operation on the server side.
The following are the related classes for C # Base64 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 description of MyBase64
</summary>
public class MyBase64
{
Public MyBase64 ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Server-side BASE64 encoding
</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>
Server-side Base64 decoding
</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 there will be an error when the server decodes

The server side uses the following code to invoke:
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.