自制加密器有时会出错字母

时间:2017-09-13 19:15:45

标签: javascript html

我正在制作一个自制文本加密器并且工作正常 - 除了有时它会错误地解密邮件这一事实。请参阅代码示例(抱歉,它非常混乱,我只是从HTML / JS开始):

要看到它出错,请输入' hi'作为消息和' moo123'作为关键。不要问为什么,当我发现问题时,我只是在尝试随机的话。然后使用相同的密钥将加密的消息复制到文本输入中,然后按解密。它说'那里''代替。

有人可以告诉我为什么会这样,以及如何解决这个问题?谢谢!



<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Encryptor</title>
        <style>
            body {
                font-family: monospace;
            }
            input {
                font-family: monospace;
                width: 400px;
            }
        </style>
    </head>
    <body>
        <h1>Encryptor</h1>
        <p>Enter the text to encrypt:</p>
        <input type="text" id="text">
        <p>Enter the encrytion key:</p>
        <input type="text" id="key"><br><br>
        <button onclick="encrypt()">Encrypt</button>
        <button onclick="decrypt()">Decrypt</button><br><br>
        <p id="output"></p>
        <p id="debug"></p>
        
        <script>
            function encrypt() {
                //console.log("----------------");
                var text = document.getElementById("text").value;
                var key = document.getElementById("key").value;
                var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&-={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&-={}:;@'~#<,>./|`¬ ";
                //var chars = "abcdefghijklmnopqrstuvwxyz"
                var output = "";
                var keyNum = 0;
                var char = "";
                var keychar = "";
                var finalchar = "";
                for (var i = 0; i < text.length; i++) {
                    char = chars.search((text.substring(i, i+1)))+87;
                    //console.log("char "+char);
                    //keychar = chars.search(key.substring(keyNum, KeyNum+1));
                    //document.getElementById("output").innerHTML = key.substring(keyNum, keyNum+1);
                    keychar = (chars.search(key.substring(keyNum, keyNum+1)));
                    //console.log("keychar "+keychar);
                    //console.log("char - keychar "+parseFloat(char)-parseFloat(keychar));
                    finalchar = chars[parseFloat(char)-parseFloat(keychar)];
                    
                    output = output + finalchar;
                    if (keyNum >= key.length-1) {
                        keyNum = 0;
                    } else {
                        keyNum++;
                    }
                    
                }
                document.getElementById("output").innerHTML = output;
            }
            
            function decrypt() {
                //console.log("----------------");
                var text = document.getElementById("text").value;
                var key = document.getElementById("key").value;
                var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ ";
                //var chars = "abcdefghijklmnopqrstuvwxyz"
                var output = "";
                var keyNum = 0;
                var char = "";
                var keychar = "";
                var finalchar = "";
                for (var i = 0; i < text.length; i++) {
                    char = chars.search((text.substring(i, i+1)));
                    //console.log("char "+char);
                    //keychar = chars.search(key.substring(keyNum, KeyNum+1));
                    //document.getElementById("output").innerHTML = key.substring(keyNum, keyNum+1);
                    keychar = (chars.search(key.substring(keyNum, keyNum+1)))+87;
                    //console.log("keychar "+keychar);
                    //console.log("char - keychar "+parseFloat(char)-parseFloat(keychar));
                    finalchar = chars[parseFloat(char)+parseFloat(keychar)];
                    
                    output = output + finalchar;
                    if (keyNum >= key.length-1) {
                        keyNum = 0;
                    } else {
                        keyNum++;
                    }
                    
                }
                document.getElementById("output").innerHTML = output;
            }
        </script>
    </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您正在使用string.prototype.search assumes a regular expression as input。由于这个原因,字符串中的某些字符无法正常工作。例如,如果您加密hi,它将返回/.,但.是&#34;任何字符&#34;所以它将返回0作为匹配。请改用indexOf

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Encryptor</title>
        <style>
            body {
                font-family: monospace;
            }
            input {
                font-family: monospace;
                width: 400px;
            }
        </style>
    </head>
    <body>
        <h1>Encryptor</h1>
        <p>Enter the text to encrypt:</p>
        <input type="text" id="text">
        <p>Enter the encrytion key:</p>
        <input type="text" id="key"><br><br>
        <button onclick="encrypt()">Encrypt</button>
        <button onclick="decrypt()">Decrypt</button><br><br>
        <p id="output"></p>
        <p id="debug"></p>
        
        <script>
            function encrypt() {
                //console.log("----------------");
                var text = document.getElementById("text").value;
                var key = document.getElementById("key").value;
                var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ ";
                //var chars = "abcdefghijklmnopqrstuvwxyz"
                var output = "";
                var keyNum = 0;
                var char = "";
                var keychar = "";
                var finalchar = "";
                for (var i = 0; i < text.length; i++) {
                    char = chars.indexOf((text.substring(i, i+1)))+87;
                    //console.log("char "+char);
                    //keychar = chars.search(key.substring(keyNum, KeyNum+1));
                    //document.getElementById("output").innerHTML = key.substring(keyNum, keyNum+1);
                    keychar = (chars.indexOf(key.substring(keyNum, keyNum+1)));
                    //console.log("keychar "+keychar);
                    //console.log("char - keychar "+parseFloat(char)-parseFloat(keychar));
                    finalchar = chars[parseFloat(char)-parseFloat(keychar)];
                    
                    output = output + finalchar;
                    if (keyNum >= key.length-1) {
                        keyNum = 0;
                    } else {
                        keyNum++;
                    }
                    
                }
                document.getElementById("output").innerHTML = output;
            }
            
            function decrypt() {
                //console.log("----------------");
                var text = document.getElementById("text").value;
                var key = document.getElementById("key").value;
                var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&_={}:;@'~#<,>./|`¬ ";
                //var chars = "abcdefghijklmnopqrstuvwxyz"
                var output = "";
                var keyNum = 0;
                var char = "";
                var keychar = "";
                var finalchar = "";
                for (var i = 0; i < text.length; i++) {
                    char = chars.indexOf((text.substring(i, i+1)));
                    //console.log("char "+char);
                    //keychar = chars.search(key.substring(keyNum, KeyNum+1));
                    //document.getElementById("output").innerHTML = key.substring(keyNum, keyNum+1);
                    keychar = (chars.indexOf(key.substring(keyNum, keyNum+1)))+87;
                    //console.log("keychar "+keychar);
                    //console.log("char - keychar "+parseFloat(char)-parseFloat(keychar));
                    finalchar = chars[parseFloat(char)+parseFloat(keychar)];
                    
                    output = output + finalchar;
                    if (keyNum >= key.length-1) {
                        keyNum = 0;
                    } else {
                        keyNum++;
                    }
                    
                }
                document.getElementById("output").innerHTML = output;
            }
        </script>
    </body>
</html>
&#13;
&#13;
&#13;

另外,请注意,如果您仔细比较它们,那么两个char字符串实际上并不相同。这可能也是一个问题,但我不确定它是否是故意的(我假设不是)? 现已更正