Implement BASE64_ENCODE and BASE64_DECODE in JS (instance code)

Source: Internet
Author: User
Tags 0xc0 base64 encode

Copy codeThe Code is as follows:
<Script language = 'javascript '>
/* Utf. js-UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999.
* This library is free. You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* Utf8 = utf16to8 (utf16 );
* Utf16 = utf16to8 (utf8 );
*/

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;
}

/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999.
* This library is free. You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* B64 = base64encode (data );
* Data = base64decode (b64 );
*/


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

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;
}

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;
}
// Input base64 encode
Function strdecode (str ){
Return utf8to16 (base64decode (str ));
}

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.