Advertisement
Guest User

Poly Cipher

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.76 KB | None | 0 0
  1.  <!DOCTYPE html>
  2. <html>
  3.    <head>
  4.       <title>Poly Ciphering</title>
  5.       <script type="text/javascript">
  6.         function getKey(){
  7.             var cycle = document.forms["mainForm"]["key"].value;
  8.             var cycleArrayStr = cycle.split(" ");
  9.             var cycleArrayInt = [];
  10.                 for(i = 0; i < cycleArrayStr.length; i++){
  11.                    cycleArrayInt =
  12. cycleArrayInt.concat(Number(cycleArrayStr[i]));
  13.                    if (Number(cycleArrayInt[i]) == NaN){
  14. return []; }
  15.                }
  16.                return cycleArrayInt;
  17.        }
  18.        function encodeMessage(message, key){
  19.            var encodedMessage = [];
  20.            var position = 0;
  21.            for(i = 0; i < message.length;i++){
  22.                if (position == key.length){
  23.                    position = 0;
  24.                }
  25.                encodedMessage =
  26. encodedMessage.concat((message.charCodeAt(i) + key[position])%128);
  27.                position ++;
  28.            }
  29.            return encodedMessage.join(" ");
  30.        }
  31.        function decodeMessage(message, key){
  32.            var valueArrayStr = message.split(" ");
  33.            var valueArrayInt = [];
  34.                for(i = 0; i < valueArrayStr.length; i++){
  35.                    valueArrayInt =
  36. valueArrayInt.concat(Number(valueArrayStr[i]));
  37.                    if (Number(valueArrayInt[i]) == NaN){
  38. return ""; }
  39.                }
  40.            var decodedMessage = [];
  41.            var position = 0;
  42.            var response = "";
  43.            for(i = 0; i < valueArrayStr.length;i++){
  44.                if (position == key.length){
  45.                    position = 0;
  46.                }
  47.                decodedMessage =
  48. decodedMessage.concat(String.fromCharCode((((valueArrayInt[i] -
  49. key[position])%128)+128)%128));
  50.                response = response.concat(decodedMessage[i]);
  51.  
  52. "encode"){
  53. if (document.forms["mainForm"]["translation"].value ===
  54.    var key = getKey();
  55.    var response = "";
  56.    if (key === []){
  57.        response = "Invalid input for cipher key";
  58.    } else{
  59.        position ++;
  60.    }
  61.    return decodedMessage.join("");
  62. }
  63. function translater(){
  64.                     response =
  65. encodeMessage(document.forms["mainForm"]["input"].value,key);
  66. }
  67.                document.forms["mainForm"]["output"].value = response;
  68.            } else {
  69.                var key = getKey();
  70.                var response = "";
  71.                if (key === []){
  72.                    response = "Invalid input for cipher key";
  73.                } else{
  74.                    response =
  75. decodeMessage(document.forms["mainForm"]["input"].value,key);
  76.                    if(response === ""){
  77.                        response = "Invalid input for decoding";
  78.                    }
  79. }
  80.                document.forms["mainForm"]["output"].value = response;
  81.            }
  82. } </script>
  83. </head>
  84. <body> <h1>
  85.             Poly Cipher
  86.         </h1>
  87.       <form name = "mainForm" autocomplete = "off">
  88.          Input the key for translating the message(Put spaces between
  89. the numbers): <input type = "text" name = "key" />
  90.          <br/>
  91.          <br/>
  92.          <hr>
  93.          <br/>
  94.          Input a message to translate:
  95.          <br/>
  96.          <textarea rows = 5 cols = 50 name = "input"></textarea>
  97.          <br/>
  98.          <br/>
  99.          Translated message:
  100.          <br/>
  101.          <textarea rows = 5 cols = 50 name = "output" readonly>
  102.  
  103. </textarea>
  104.          <br/>
  105.          <input type = "radio" name = "translation" value = "encode"
  106. checked> Encode Message
  107.          <input type = "radio" name = "translation" value = "decode">
  108. Decode Message
  109.          <br/>
  110.          <br/>
  111.          <input type = "button" onclick = "translater();" name =
  112. "btnTranslate"  value = "Translate" />
  113.       </form>
  114.    </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement