用Javascript實現數據加密解密效果

加密.hta代碼(完全可以保存爲html,執行效果一樣)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>設置加密郵件內容</title>
<script type="text/javascript">
function itArt(){
                this.html="Hello";
                this.css="color:#000;width:640px;text-indent:20px;padding:5px;border:solid 5px #666;margin:5px;background:#CCC;margin-left:auto;margin-right:auto";
                this.length=0;
}

itArt.prototype = {
                init:function(){
                                this.length=this.html.length;
                },
                play:function(){
                                this.init();
                                this.setPassword();
                                this.echo();
                },
                setHtml:function(v){
                                this.html=v;
                },
                setPassword:function(){
                                var tmp="";
                                for(var i=0;i<this.length;i++){
                                                tmp+="$"+this.html.charCodeAt(i);
                                }
                                this.html=tmp;
                                tmp="";
                                for(var i=0;i<this.html.length;i+=3){
                                                tmp+=" "+this.html.substr(i,3);
                                }
                                this.html=tmp;
                                tmp="";
                                for(var i=0;i<this.html.length;i++){
                                                tmp+=" "+this.html.charCodeAt(i);
                                }
                                this.html=tmp;
                },
                echo:function(){
                                var t=document.createElement("div");
                                t.innerHTML=this.html;
                                if(window.ActiveXObject)
                                                t.style.cssText=this.css;
                                else
                                                t.setAttribute("style",this.css);
                                //不想用DOM刪掉子元素了,反正各瀏覽器都支持
                                document.getElementById("x100").innerHTML="";
                                document.getElementById("x100").appendChild(t);
                }
}
function setPassword(t){
                if(t!=""){
                                var demo=new itArt();
                                demo.setHtml(t);
                                demo.play();
                }else{
                                alert("請輸入內容開始加密");
                }
}
window.
                setPassword("你好,猜猜我給你的密文是什麼? 哈哈,猜不到吧");
}
</script>
</head>
<body>
<textarea style="display:block;color:#000;width:640px;height:280px;text-indent:20px;border:solid 5px #666;margin:5px;background:#FFF;margin-left:auto;margin-right:auto" ondblclick="setPassword(this.value);" title="雙擊開始加密">你好,猜猜我給你的密文是什麼? 哈哈,猜不到吧</textarea>
<div id="x100">
</div>
</body>
</html>
 
 
解密
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>獲取加密郵件內容</title>
<script type="text/javascript">
function itArt(){
                this.html="Hello";
                this.css="color:#000;width:640px;text-indent:20px;padding:5px;border:solid 5px #666;margin:5px;background:#CCC;margin-left:auto;margin-right:auto";
                this.length=0;
}

itArt.prototype = {
                init:function(){
                                this.length=this.html.length;
                },
                play:function(){
                                this.init();
                                this.setPassword();
                                this.echo();
                },
                setHtml:function(v){
                                this.html=v;
                },
                setPassword:function(){
                                var tmp=this.html.split(" "),tmps="";
                                for(i=0;i<tmp.length;i++){
                                                tmps+=String.fromCharCode(tmp[i]);
                                }
                                this.html=tmps.replace(" ","");
                                tmps="";
                                tmp=this.html.split("$");
                                for(i=0;i<tmp.length;i++){
                                                if(tmp[i]!=""&&tmp[i]!=0){
                                                                tmps+=String.fromCharCode(tmp[i].replace(" ",""));
                                                }
                                }
                                this.html=tmps;
                },
                echo:function(){
                                var t=document.createElement("div");
                                t.innerHTML=this.html;
                                if(window.ActiveXObject)
                                                t.style.cssText=this.css;
                                else
                                                t.setAttribute("style",this.css);
                                //不想用DOM刪掉子元素了,反正各瀏覽器都支持
                                document.getElementById("x100").innerHTML="";
                                document.getElementById("x100").appendChild(t);
                }
}

demo=new itArt();
demo.setHtml();
function getPassword(t){
                if(t!=""){
                var demo=new itArt();
                demo.setHtml(t);
                demo.play();
                }else{
                                alert("請輸入內容開始解密");
                }
}
window.
                getPassword("32 36 50 48 32 51 50 48 32 36 50 50 32 57 48 57 32 36 54 53 32 50 57 50 32 36 50 57 32 52 54 56 32 36 50 57 32 52 54 56 32 36 50 53 32 49 48 53 32 36 51 50 32 52 55 51 32 36 50 48 32 51 50 48 32 36 51 48 32 51 52 48 32 36 50 51 32 52 57 52 32 36 50 53 32 57 57 49 32 36 50 54 32 49 53 57 32 36 50 48 32 49 54 48 32 36 50 48 32 48 52 48 32 36 54 53 32 51 49 49 32 36 51 50 32 36 50 49 32 55 48 52 32 36 50 49 32 55 48 52 32 36 54 53 32 50 57 50 32 36 50 57 32 52 54 56 32 36 49 57 32 57 56 49 32 36 50 49 32 48 52 48 32 36 50 49 32 53 52 51");
}
</script>
</head>
<body>
<textarea style="display:block;color:#000;width:640px;height:280px;text-indent:20px;border:solid 5px #666;margin:5px;background:#FFF;margin-left:auto;margin-right:auto" ondblclick="getPassword(this.value);" title="雙擊開始解密">32 36 50 48 32 51 50 48 32 36 50 50 32 57 48 57 32 36 54 53 32 50 57 50 32 36 50 57 32 52 54 56 32 36 50 57 32 52 54 56 32 36 50 53 32 49 48 53 32 36 51 50 32 52 55 51 32 36 50 48 32 51 50 48 32 36 51 48 32 51 52 48 32 36 50 51 32 52 57 52 32 36 50 53 32 57 57 49 32 36 50 54 32 49 53 57 32 36 50 48 32 49 54 48 32 36 50 48 32 48 52 48 32 36 54 53 32 51 49 49 32 36 51 50 32 36 50 49 32 55 48 52 32 36 50 49 32 55 48 52 32 36 54 53 32 50 57 50 32 36 50 57 32 52 54 56 32 36 49 57 32 57 56 49 32 36 50 49 32 48 52 48 32 36 50 49 32 53 52 51</textarea>
<div id="x100">
</div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章