var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -); function base64encode(str) {
var out, i, len;
var c1, c2, c3; len = str.length;
i = ;
out = "";
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt((c1 & 0x3) << );
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt(((c1 & 0x3)<< ) | ((c2 & 0xF0) >> ));
out += base64EncodeChars.charAt((c2 & 0xF) << );
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt(((c1 & 0x3)<< ) | ((c2 & 0xF0) >> ));
out += base64EncodeChars.charAt(((c2 & 0xF) << ) | ((c3 & 0xC0) >>));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
} function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out; len = str.length;
i = ;
out = "";
while(i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c1 == -);
if(c1 == -)
break; /* c2 */
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c2 == -);
if(c2 == -)
break; out += String.fromCharCode((c1 << ) | ((c2 & 0x30) >> )); /* c3 */
do {
c3 = str.charCodeAt(i++) & 0xff;
if(c3 == )
return out;
c3 = base64DecodeChars[c3];
} while(i < len && c3 == -);
if(c3 == -)
break; out += String.fromCharCode(((c2 & 0XF) << ) | ((c3 & 0x3C) >> )); /* c4 */
do {
c4 = str.charCodeAt(i++) & 0xff;
if(c4 == )
return out;
c4 = base64DecodeChars[c4];
} while(i < len && c4 == -);
if(c4 == -)
break;
out += String.fromCharCode(((c3 & 0x03) << ) | c4);
}
return out;
} function utf16to8(str) {
var out, i, len, c; out = "";
len = str.length;
for(i = ; 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 >> ) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> ) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
}
}
return out;
} function utf8to16(str) {
var out, i, len, c;
var char2, char3; out = "";
len = str.length;
i = ;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> )
{
case : case : case : case : case : case : case : case :
// 0xxxxxxx
out += str.charAt(i-);
break;
case : case :
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << ) | (char2 & 0x3F));
break;
case :
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << ) |
((char2 & 0x3F) << ) |
((char3 & 0x3F) << ));
break;
}
} return out;
} function CharToHex(str) {
var out, i, len, c, h;
out = "";
len = str.length;
i = ;
while(i < len)
{
c = str.charCodeAt(i++);
h = c.toString();
if(h.length < )
h = "" + h; out += "\\x" + h + " ";
if(i > && i % == )
out += "\r\n";
} return out;
} function doEncode() {
var src = document.getElementById('src').value;
document.getElementById('dest').value = base64encode(utf16to8(src)); //调用这里加密
} function doDecode() {
var src = document.getElementById('src').value;
var opts = document.getElementById('opt'); if(opts.checked)
{
document.getElementById('dest').value = CharToHex(base64decode(src)); //调用这里解密
}
else
{
document.getElementById('dest').value = utf8to16(base64decode(src));
}
}
//window.BASE64 = __BASE64;
function ss(){
return '';
}
js base64加密解密