经常浏览别人的网页时,会看到重要的信息就被加密混淆了,常见的就类似\\u4faf\\u6587\\u658c 、\u4faf\u6587\u658c、侯文斌等转换的字符串,相信正常人直接不会看懂的(火星来的除外:)! )其实这些就是unicode编码,只要知道这点我们就可以很轻松地转换成我们能看懂的明文了,今天我们来看看这个JS代码:
<script type="text/javascript">
var toHTML = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = str.charCodeAt(i++);
return "&#" + a.join(";&#") + ";"
},
un: function(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g,
function(a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10))
})
}
};
//alert(toHTML.on("侯文斌"));
//alert(toHTML.un("侯文斌"));
var toUN = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = ("00" + str.charCodeAt(i++).toString(16)).slice( - 4);
return "\\u" + a.join("\\u")
},
un: function(str) {
return unescape(str.replace(/\\/g, "%"))
}
};
//alert(toUN.on("侯文斌"));
//alert(toUN.un("\u4faf\u6587\u658c"));
//alert(toUN.un("\\u4faf\\u6587\\u658c"));
</script>
呵呵,很简单的代码,就实现了互转哦!
页面源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript之unicode编码转换</title>
<style>
.but2{ border:1px solid #c5e2f2; background:#cde4f2 repeat-x 50% top; height:30px; margin-left:5px; cursor:pointer; margin-bottom:5px;width:120px;}
</style>
</head>
<body style="margin:20px auto; background-color:#333">
<script type="text/javascript">
var toHTML = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = str.charCodeAt(i++);
return "&#" + a.join(";&#") + ";"
},
un: function(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g,
function(a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10))
})
}
};
var toUN = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = ("00" + str.charCodeAt(i++).toString(16)).slice( - 4);
return "\\u" + a.join("\\u")
},
un: function(str) {
return unescape(str.replace(/\\/g, "%"))
}
};
</script>
<div style="text-align:center; width:500px; margin:0 auto;">
<div style="text-align:center; color:#FFF; width:500px; height:30px">JS-&#unicode;编码转换------------------------JS-unicode编码转换</div>
<span style="float:inherit">
<textarea name="content" id="content" style=" border:1px solid #c5e2f2; width:500px; height:250px;">请把你需要加密的内容粘贴在这里!</textarea>
</span>
<span style="float:none; height:1px"></span>
<div style="width:500px; height:15px"></div>
<span style="float:center;">
<input type="button" class="but2" οnclick="content.value=toHTML.on(content.value);" value="JS-&#unicode;加密" />
<input type="button" class="but2" οnclick="content.value=toHTML.un(content.value);" value="JS-&#unicode;解密" />
<div style="height:1px"></div>
<input type="button" class="but2" οnclick="content.value=toUN.on(content.value);" value="JS-unicode;加密" />
<input type="button" class="but2" οnclick="content.value=toUN.un(content.value);" value="JS-unicode;解密" />
</span>
</div>
</body>
</html>