欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JS里charCodeAt()和fromCharCode()方法拓展应用:加密与解密

程序员文章站 2022-04-10 16:04:23
...
JS实现客户端的网页加密解密技术,可用作选择性隐蔽展示。当然客户端的加密安全度是不能与服务器相提并论,肯定不能用于密码这类内容的加密,但对于一般级别的内容用作展示已经够了。

JS加密与解密的解决方案有很多,本文则利用String对象的charCodeAt()方法和fromCharCode()方法对字符的ASCII编码进行获取和修改。

加密,解密代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5 
 6     <title>网页加密及解密</title>
 7     <meta name="author" content="xiongzaiqiren" />
 8     <meta name="keywords" content="" />
 9     <meta name="description" content="" />
10     <meta name="generator" content="ClassBao team coding in July 10, 2017" />
11 
12 </head>
13 
14 <body>
15     <p><textarea id="text1" name="textfield" cols="50" rows="5">钱庄王员外这个人怎么样?</textarea></p>
16     <input type="button" name="Button1" value="加密" onClick="text1.value = MySign.Encrypt(text1.value);">
17     <input type="button" name="Button2" value="解密" onClick="text1.value = MySign.UnEncrypt(text1.value);">
18 
19     <script language="JavaScript">
20         var MySign = {
21             //加密/解密次数
22             num: 0,
23             //加密
24             Encrypt: function (Text) {
25                 this.num = this.num + 1;
26                 output = new String;
27                 alterText = new Array();
28                 varCost = new Array();
29                 TextSize = Text.length;
30                 for (i = 0; i < TextSize; i++) {
31                     idea = Math.round(Math.random() * 111) + 77;
32                     alterText[i] = Text.charCodeAt(i) + idea;
33                     varCost[i] = idea;
34                 }
35                 for (i = 0; i < TextSize; i++) {
36                     output += String.fromCharCode(alterText[i], varCost[i]);
37                 }
38                 //text1.value = output;
39                 return output;
40             },
41 
42             //解密
43             UnEncrypt: function (Text) {
44                 if (this.num > 0) {
45                     this.num = this.num - 1;
46                     output = new String;
47                     alterText1 = new Array();
48                     varCost1 = new Array();
49                     TextSize = Text.length;
50                     for (i = 0; i < TextSize; i++) {
51                         alterText[i] = Text.charCodeAt(i);
52                         varCost[i] = Text.charCodeAt(i + 1);
53                     }
54                     for (i = 0; i < TextSize; i = i + 2) {
55                         output += String.fromCharCode(alterText[i] - varCost[i]);
56                     }
57                     //text1.value = output;
58                     return output;
59                 }
60             }
61         };
62 
63 
64         //测试代码
65         var testString = "光头强,还不去砍树?";
66         console.log(testString);
67 
68         var sign = MySign.Encrypt(testString); //凑ˆ妣o忕›ァ[還¬什³呯´硠S桲aチb
69         var sign2 = MySign.UnEncrypt(sign); //光头强,还不去砍树?
70 
71         console.log(sign);
72         console.log(sign2);
73 
74     </script>
75 
76 </body>
77 </html>

 

以上就是JS里charCodeAt()和fromCharCode()方法拓展应用:加密与解密的详细内容,更多请关注其它相关文章!