简体   繁体   English

Javascript文本数组数据加扰和解扰

[英]Javascript text Array Data Scrambling and Descrambling

I am looking for super fast and compact Javascript code or function to scramble and descramble text stored in Arrays. 我正在寻找超快速和紧凑的Javascript代码或函数来对存储在数组中的文本进行加扰和解密。

I only want this text not readable when the user go into "View Source" mode with the browser. 当用户使用浏览器进入“查看源代码”模式时,我只希望此文本不可读。

There are many options like add fixed numbers to the ASCII code or do some boolean calculation on the string like shifting, reversing, change to octal, hex etc. 有很多选项,例如将固定数字添加到ASCII代码或对字符串进行一些布尔计算,例如移位,反转,更改为八进制,十六进制等。

I need this both for text and number strings. 我需要文本和数字字符串。 It would be best if the scrambled code where not to complex and not with sign like ", ', #, $, &, / etc. 最好是将加扰的代码不复杂且不带有“,”,#,$,&,/等符号。

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]);
        }   
    }
});

Any good idea? 有什么好主意吗?

How about ROT13 , ROT47, or some other substitution cipher? ROT13 ,ROT47或其他替代密码怎么样? It's simple to implement, fast, and doesn't increase the length of the string. 它实现简单,快速且不会增加字符串的长度。

If you are scramble the text the user will be able to see the data being transmitted. 如果您正在加扰文本,则用户将能够看到正在传输的数据。 If you only want to use JS and make it universal perhaps have the server send that data as UTF8 bytes? 如果您只想使用JS并使其通用,那么服务器是否可以将数据作为UTF8字节发送? Also this does not fall under encryption. 同样,这也不属于加密范围。 Encryption and Obfuscation are not the same. 加密和混淆不同。 I provided a link to a github file that does base and data type conversions. 我提供了指向进行基本和数据类型转换的github文件的链接。 I assume you where looking for something like this. 我想你在找这样的东西。

https://github.com/CubanAzcuy/JSBytes/blob/master/Format.js https://github.com/CubanAzcuy/JSBytes/blob/master/Format.js

(All Byte to UTF8 String Operations are done as unsigned bytes) (I agree with @mishik minify(ing) your code is one of the best ways to obfuscate) (所有字节到UTF8字符串的操作均以无符号字节的形式进行)(我同意@mishik minify(ing),您的代码是混淆的最佳方法之一)

I made this working solution to solve my problem: 我提出了这个可行的解决方案来解决我的问题:

There are few things in this code I would like to work out better if possible. 如果可能的话,这段代码中我希望能做得更好。

1) In line 33 I have to use this dirty trick ""+c[i][j]) to convert number in Array to string. 1)在第33行中,我必须使用这个肮脏的把戏""+c[i][j])将Array中的数字转换为字符串。 But when going back the problem is number is not number anymore in my array! 但是当返回时,问题是我的数组中的数字不再是数字! This is very fast but if you have any better idea without loosing the number definition then pls. 这非常快,但是如果您有更好的主意而又不松懈数字定义,那么请。 let me know. 让我知道。 2) I am using 2 version ROT13 and ROT18. 2)我正在使用2个版本的ROT13和ROT18。 I found ROT13 one-line-code version: s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}); 我发现ROT13是单行代码版本: s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}); How can I add numbers and -_@ letters to those ROT13 in a simple way? 如何以简单的方式向那些ROT13添加数字和-_ @字母? 3) As you can see I am using 2D Array to store my code. 3)如您所见,我正在使用2D数组存储代码。 Any better suggestion? 有更好的建议吗?

<!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>

I put copy of the code on http://jsfiddle.net/kpsphoto/b7MaQ/ 我将代码副本放在http://jsfiddle.net/kpsphoto/b7MaQ/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM