我正在寻找超快速和紧凑的Javascript代码或函数来对存储在数组中的文本进行加扰和解扰。
当用户使用浏览器进入“查看源”模式时,我只希望此文本无法读取。
有许多选项,例如在ASCII代码中添加固定数字或对字符串进行一些布尔计算,如移位,反转,更改为八进制,十六进制等。
我需要这个文本和数字字符串。如果扰乱的代码不复杂而不是像“,”,#,$,&,等等那样的符号,那将是最好的。
var c = new Array();
c[0]=new Array( "Name","Home","City","Post code","Telephone","email","Web","Id","Number","xpos","ypos");
c[1]=new Array( "John","Street 123","1234","New York","555-1450123","john@demo.com","www.demo1.com","b",59,306380,565500);
c[2]=new Array( "Poul","Street 1234","2345","New York","555-7010123","poul@demo.com","www.demo2.com","i",113,308396,635477);
c[3]=new Array( "David","Street 12345","3456","New York","555-3111123","david@demo.com","www.demo3.com","i",129,377615,581358);
var Scrambler = function(n) { return ASCII(n)+1...; }
var DeScrambler = function(n) { return ASCII(n)-1...; }
$(function() {
for (var i = 0; i < c[0].length; ++i) {
for (var j = 0; j < (c.length); ++i) {
a[j][i] = DeScrambler(c[j][i]);
}
}
});
有什么好主意吗?
答案 0 :(得分:1)
ROT13,ROT47或其他替换密码怎么样?它实现起来很简单,速度快,并且不会增加字符串的长度。
答案 1 :(得分:0)
如果您对文本进行加扰,则用户将能够看到正在传输的数据。如果您只想使用JS并使其通用,那么服务器是否可以将该数据作为UTF8字节发送?这也不属于加密。加密和混淆不一样。我提供了一个github文件的链接,该文件执行基本和数据类型转换。我猜你在哪里寻找这样的东西。
https://github.com/CubanAzcuy/JSBytes/blob/master/Format.js
(所有字节到UTF8字符串操作都以无符号字节完成) (我同意@mishik minify(ing)你的代码是混淆的最佳方法之一)
答案 2 :(得分:0)
我做了这个有效的解决方案来解决我的问题:
如果可能的话,我希望在这段代码中找到更好的东西。
1)在第33行中,我必须使用这个脏技巧""+c[i][j])
将Array中的数字转换为字符串。但回过头来问题就是我的数组中的数字不再是数字了!这是非常快的,但如果你有任何更好的主意而不丢失数字定义,那么请。让我知道。
2)我使用的是2版ROT13和ROT18。我找到了ROT13单行代码版本:s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
如何以简单的方式为这些ROT13添加数字和-_ @字母?
3)正如您所看到的,我正在使用2D Array来存储我的代码。还有更好的建议吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
var AscII = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-";
var ROT18 = "STUVWXYZ0123456789@.-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR";
var c = new Array();
c[0]=new Array( "Name","Home","City","Post code","Telephone","email","Web","Id","Number","xpos","ypos");
c[1]=new Array( "John","Street 123","1234","New York","438-1450123","john@demo.com","www.demo1.com","b",59,306380,565500);
c[2]=new Array( "Poul","Street 1234","2345","New York","450-7010123","poul@demo.com","www.demo2.com","i",113,308396,6354772);
c[3]=new Array( "David","Street 12345","3456","New York","451-3111123","david@demo.com","www.demo3.com","i",129,377615,581358);
$(function() {
var Normal = function() {
var txt ="";
for (var i = 0; i < c.length; ++i) {
for (var j = 0; j < c[0].length; ++j) {
txt += ""+c[i][j] + ", ";
}
txt += "<br>";
}
$("#kData").html("<b>Normal ASCII<br></b>" + txt);
};
var Convert18 = function(Div, TxtFrom, TxtTo) {
var txt ="";
for (var i = 0; i < c.length; ++i) {
for (var j = 0; j < c[0].length; ++j) {
var ktxtX = decode((""+c[i][j]), TxtFrom, TxtTo);
c[i][j] = ktxtX;
txt += ktxtX + ", ";
}
txt += "<br>";
}
$(Div).html("<b>ROT18 + Numbers + @-_<br></b>" + txt);
};
var Convert13 = function(Div) {
var txt ="";
for (var i = 0; i < c.length; ++i) {
for (var j = 0; j < c[0].length; ++j) {
var ktxtX = rot13(""+c[i][j]);
// s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
c[i][j] = ktxtX;
txt += ktxtX + ", ";
}
txt += "<br>";
}
$(Div).html("<b>ROT13<br></b>" + txt);
};
var decode = function (txt, alphabet, substitution) {
return txt.split("").map(function (c) {
if (alphabet.indexOf(c) != -1) { return substitution.charAt(alphabet.indexOf(c)); }
else { return c; }
}).join("");
};
function rot13(str) {
return str.replace(/[a-zA-Z]/g, function(c) {
return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
});
}
Normal();
Convert18("#kData2", AscII, ROT18);
Convert18("#kData3", ROT18, AscII);
Convert13("#kData4");
Convert13("#kData5");
$("#kData6").html("Finised - j:" + c[0].length + " - i:" + c.length);
});
</script>
</head>
<body>
<div id="kData"></div>
<div id="kData2"></div>
<div id="kData3"></div>
<div id="kData4"></div>
<div id="kData5"></div>
<div id="kData6"></div>
</body>
</html>
我将代码的副本放在http://jsfiddle.net/kpsphoto/b7MaQ/
上