后台保存cookies时加密,下面是asp后台的例子:
Function CodeCookie(str)
Dim strRtn
For i = Len(str) To 1 step - 1
strRtn = strRtn & AscW(Mid(str,i,1))
If (i<>1) Then strRtn = strRtn & "a"
Next
CodeCookie = strRtn
End Function
下面是客户端页面读取cookies并解密的js代码:
<script language="JavaScript">
function DecodeCookie(str) {
var strArr;
var strRtn="";
strArr=str.split("a");
for (var i=strArr.length-1;i>=0;i--)
strRtn+=String.fromCharCode(eval(strArr[i]));
return strRtn;
}
function GetCookie(sMainName, sSubName){
var sCookieName = sMainName + "=";
var sSubCookieName = (sSubName) ? sSubName + "=" : null;
var sCookie;
var sWholeCookie = document.cookie;
var nValueBegin = sWholeCookie.indexOf(sCookieName);
if(nValueBegin != -1){
var nValueEnd = sWholeCookie.indexOf(";", nValueBegin);
if (nValueEnd == -1) nValueEnd = sWholeCookie.length;
var sValue = sWholeCookie.substring(nValueBegin + sCookieName.length, nValueEnd);//获得Cookie值
if(sSubCookieName){//多值Cookie
var nSubValueBegin = sValue.indexOf(sSubCookieName);
if(nSubValueBegin != -1){
var nSubValueEnd = sValue.indexOf("&", nSubValueBegin);
if(nSubValueEnd == -1) nSubValueEnd = sValue.length;
var sSubValue = sValue.substring(nSubValueBegin + sSubCookieName.length, nSubValueEnd);//获得指定的子键值
return unescape(sSubValue);
}
}
if(!sSubCookieName) return unescape(sValue);
}
return null;
}
alert(DecodeCookie(GetCookie("MyReg","Stop1")));
</script>

