1.编码与解码
(1)使用escape与unescape 进行编码与解码
escape()是一种不完全解码方式,仅仅将字符串中某些字符替换成十六进制的转义序列。
具体点说,除了ASCII字母,数字和标点符号(如@,*,_,+,-,\)之外,所有的字符都被转换成
%xx或%uxxx(x代表十六进制的数字)的转义序列.
例如:
代码
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
js 字符串加密与解密
</
title
>
<
script
type
="text/javascript"
>
var
s
=
"
javascript中国
"
;
s
=
escape(s);
alert(s);
//
javascript%u4E2D%u56FD
//
document.write(s);
s
=
unescape(s);
alert(s);
//
javascript中国
//
document.write(s);
</
script
>
</
head
>
<
body
>
</
body
>
</
html
>
(2)使用encodeURI与decodeURI
encodeURI()对url字符串进行转义处理,测试如下:
代码
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
js 字符串加密与解密
</
title
>
<
script
type
="text/javascript"
>
var
s
=
"
javascript中国
"
;
s
=
encodeURI(s);
alert(s);
//
javascript%E4%B8%AD%E5%9B%BD
document.write(s);
s
=
decodeURI(s);
alert(s);
//
javascript中国
document.write(s);
</
script
>
</
head
>
<
body
>
</
body
>
</
html
>
encodeURI()的结果与escape()不同,但是,与escape()相同的是,对于ASCII,字和标点符号(如@,*,_,+,-,\)
都不会被编码。相对于escape(),encodeURI()更加安全,它能将字符串转换为UTF-8编码字符,然后用十六进制的转义序列生成一个,两个,或者三个字节的字符编码。在这种编码模式中,ASCII字符由一个%xx转义字符替换,在\u0080~u07ff之间的编码由两个转义字符替换,其他的16位unicode字符由3个转义序列替换。使用decodeURI()进行解码
(3)使用encodeURIComponent与decodeURIComponent
encodeURIComponent()与encodeURI()的不同在于,encodeURIComponent()假定参数是url的一部分,例如,协议,主机名,路径或者查询字符串。因此,encodeURIComponent()将转义用于分隔url各个部分的标点符号。而encodeURI()方法仅把他们视为普通的ASCII,并没有转换。
代码
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
js 字符串加密与解密
</
title
>
<
script
type
="text/javascript"
>
var
t
=
"
http://www.cnblogs.com/dooom?keyword=url
"
;
var
a
=
encodeURI(t);
document.write(a);
document.write(
"
<br>
"
);
alert(a);
//
http://www.cnblogs.com/dooom?keyword=url
var
b
=
encodeURIComponent(t);
alert(b);
//
http%3A%2F%2Fwww.cnblogs.com%2Fdooom%3Fkeyword%3Durl
document.write(b);
</
script
>
</
head
>
<
body
>
</
body
>
</
html
>
(4)Unicode解码
如下:
代码
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
js 字符串加密与解密
</
title
>
<
script
type
="text/javascript"
>
function
unicode(s){
var
len
=
s.length;
var
rs
=
""
;
for
(
var
i
=
0
;i
<
len;i
++
){
var
k
=
s.substring(i,i
+
1
);
rs
+=
"
&#
"
+
s.charCodeAt(i)
+
"
;
"
;
}
return
rs;
}
function
runicode(s){
var
k
=
s.split(
"
;
"
);
var
rs
=
""
;
for
(i
=
0
;i
<
k.length;i
++
){
var
m
=
k[i].replace(
/
&#
/
,
""
);
rs
+=
String.fromCharCode(m);
}
return
rs;
}
var
s
=
"
javascript中国
"
;
var
b
=
unicode(s);
document.write(b);
document.write(
"
<br>
"
);
</
script
>
</
head
>
<
body
>
</
body
>
</
html
>
2.自定义加密与解密
(1)加密实现
(2)解密实现
代码
var
fromCode
=
function
(str){
var
key
=
"
0123456789abcdefghijklmnopqresuvwxyx
"
;
//
定义密钥 36个字母与数字
var
l
=
key.length;
s
=
new
array(math.floor(str.length
/
3))
b
=
s.length;
for
(
var
i
=
0
;i
<
b;i
++
)
{
b1
=
key.indexof(str.charat(d));
d
++
;
b2
=
key.indexof(str.charat(d));
d
++
;
b3
=
key.indexof(str.charat(d));
d
++
;
s[i]
=
b1
*
l
*
l
+
b2
*
l
+
b3
}
b
=
eval(
"
string.fromcharCode(
"
+
s.join(
''
,)
+
"
)
"
);
return
b;
}