JSbase64加密和后台base64解密(防⽌中⽂乱码)
直接上代码
1,js(2个⽂件,⽹上的) 不要觉的长,直接复制下来就OK
//UnicodeAnsi.js⽂件
//把Unicode转成Ansi和把Ansi转换成Unicode
function UnicodeChr() {
return'00A4,00A7,00A8,00B0,00B1,00B7,00D7,00E0,00E1,00E8,00E9,00EA,00EC,00ED,00F2,00F3,00F7,00F9,00FA,00FC,0101,0113,011B,012B,014D,016B,01CE,01D0,01D2,01D4,01D6,01D8,01DA,01DC,02C7,02C9,0391,0392,0393,0394,0 }
function AnsicodeChr() {
return'A1E8,A1EC,A1A7,A1E3,A1C0,A1A4,A1C1,A8A4,A8A2,A8A8,A8A6,A8BA,A8AC,A8AA,A8B0,A8AE,A1C2,A8B4,A8B2,A8B9,A8A1,A8A5,A8A7,A8A9,A8AD,A8B1,A8A3,A8AB,A8AF,A8B3,A8B5,A8B6,A8B7,A8B8,A1A6,A1A5,A6A1,A6 }
function UnicodeToAnsi(chrCode) {
var chrHex = String(16);
chrHex = "000" + UpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = UnicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = AnsicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
function AnsiToUnicode(chrCode) {
var chrHex = String(16);
chrHex = "000" + UpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = AnsicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = UnicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
//将Unicode编码的字符串,转换成Ansi编码的字符串
function strUnicode2Ansi(asContents) {
var len1 = asContents.length;
var temp = "";
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc < 0)
varasc += 65536;
if (varasc > 127)
varasc = UnicodeToAnsi(varasc);
if (varasc > 255) {
var varlow = varasc & 65280;
varlow = varlow >> 8;
var varhigh = varasc & 255;
temp += String.fromCharCode(varlow) + String.fromCharCode(varhigh);
}
else {
temp += String.fromCharCode(varasc);
}
}
return temp;
}
//将Ansi编码的字符串,转换成Unicode编码的字符串
function strAnsi2Unicode(asContents) {
var len1 = asContents.length;
var temp = "";
var chrcode;
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc > 127) {
chrcode = AnsiToUnicode((varasc << 8) + asContents.charCodeAt(++i));
}
else {
chrcode = varasc;
}
temp += String.fromCharCode(chrcode);
}
return temp;
}
可以将上⾯⽂件写成 xx.js 在页⾯中直接引⽤
2
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
//将Ansi编码的字符串进⾏Base64编码
function encode64(input) {
var output = "";
js代码加密软件var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
//将Base64编码字符串转换成Ansi编码的字符串
function decode64(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
if (input.length % 4 != 0) {
return"";
}
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if ((input)) {
return"";
}
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output += String.fromCharCode(chr2);
}
if (enc4 != 64) {
output += String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
//var de=encode64(strUnicode2Ansi("中国⼈"))
//var en=strAnsi2Unicode(decode64(de));
//document.writeln(de+"<br>");
//document.writeln(en+"<br>");
3 如何使⽤
var bt = encode64(strUnicode2Ansi(biaoti.val())); js 对⽂本加密
4 后台解密
///<summary>
///服务器端Base64解码
/
//</summary>
///<param name="data"></param>
///<returns></returns>
public string base64Decode(string data)
{
try
{
byte[] datas = System.Convert.FromBase64String(data);
string strPath = System.Text.Encoding.GetEncoding("gb2312").GetString(datas);
return strPath;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
5 使⽤⽅法
string bt = base64Decode(biaoti.Trim());
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论