javascript - Home-made encryptor sometimes gets letters wrong -
i'm making homemade text encryptor , works fine - apart fact decrypts message incorrectly. please see code example (sorry it's messy, i'm starting html/js):
to see make mistake, type 'hi there' message , 'moo123' key. don't ask why, trying random words when found issue. copy encrypted message text input same key press decrypt. says 'ho there' instead.
can tell me why happening , how fix it? thank you!
<!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 text encrypt:</p> <input type="text" id="text"> <p>enter 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 = ""; (var = 0; < 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 = ""; (var = 0; < 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>
you using string.prototype.search
assumes regular expression input. of characters in string not work because of this. example, if encrypt hi
return /.
.
"any character" return 0
match. use indexof
instead.
<!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 text encrypt:</p> <input type="text" id="text"> <p>enter 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 = ""; (var = 0; < 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 = ""; (var = 0; < 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>
also, note 2 char strings aren't identical if closely compare them. might issue, i'm not sure if intentional (i assume not)? now corrected
Comments
Post a Comment