Page 1/2 of common JS encryption algorithm code

Source: Internet
Author: User
Tags 0xc0

I. UTF8 encoding Functions
Copy codeThe Code is as follows:
Function URLEncode (Str ){
If (Str = null | Str = "")
Return "";
Var newStr = "";
Function toCase (sStr ){
Return sStr. toString (16). toUpperCase ();
}
For (var I = 0, icode, len = Str. length; I <len; I ++ ){
Icode = Str. charCodeAt (I );
If (icode <0x10)
NewStr + = "% 0" + icode. toString (16). toUpperCase ();
Else if (icode <0x80 ){
If (icode = 0x20)
NewStr + = "+ ";
Else if (icode> = 0x30 & icode <= 0x39) | (icode> = 0x41 & icode <= 0x5A) | (icode> = 0x61 & icode <= 0x7A ))
NewStr + = Str. charAt (I );
Else
NewStr + = "%" + toCase (icode );
}
Else if (icode <0 x800 ){
NewStr + = "%" + toCase (0xC0 + (icode> 6 ));
NewStr + = "%" + toCase (0x80 + icode % 0x40 );
}
Else {
NewStr + = "%" + toCase (0xE0 + (icode> 12 ));
NewStr + = "%" + toCase (0x80 + (icode> 6) % 0x40 );
NewStr + = "%" + toCase (0x80 + icode % 0x40 );
}
}
Return newStr;
}

Ii. Base64 encoding and decoding Functions
Copy codeThe Code is as follows:
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;
}
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;
}
Function doit (){
Var f = document. f
F. output. value = base64encode (utf16to8 (f. source. value ))
F. decode. value = utf8to16 (base64decode (f. output. value ))
}

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.