Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Poly Ciphering</title>
- <script type="text/javascript">
- function getKey(){
- var cycle = document.forms["mainForm"]["key"].value;
- var cycleArrayStr = cycle.split(" ");
- var cycleArrayInt = [];
- for(i = 0; i < cycleArrayStr.length; i++){
- cycleArrayInt =
- cycleArrayInt.concat(Number(cycleArrayStr[i]));
- if (Number(cycleArrayInt[i]) == NaN){
- return []; }
- }
- return cycleArrayInt;
- }
- function encodeMessage(message, key){
- var encodedMessage = [];
- var position = 0;
- for(i = 0; i < message.length;i++){
- if (position == key.length){
- position = 0;
- }
- encodedMessage =
- encodedMessage.concat((message.charCodeAt(i) + key[position])%128);
- position ++;
- }
- return encodedMessage.join(" ");
- }
- function decodeMessage(message, key){
- var valueArrayStr = message.split(" ");
- var valueArrayInt = [];
- for(i = 0; i < valueArrayStr.length; i++){
- valueArrayInt =
- valueArrayInt.concat(Number(valueArrayStr[i]));
- if (Number(valueArrayInt[i]) == NaN){
- return ""; }
- }
- var decodedMessage = [];
- var position = 0;
- var response = "";
- for(i = 0; i < valueArrayStr.length;i++){
- if (position == key.length){
- position = 0;
- }
- decodedMessage =
- decodedMessage.concat(String.fromCharCode((((valueArrayInt[i] -
- key[position])%128)+128)%128));
- response = response.concat(decodedMessage[i]);
- "encode"){
- if (document.forms["mainForm"]["translation"].value ===
- var key = getKey();
- var response = "";
- if (key === []){
- response = "Invalid input for cipher key";
- } else{
- position ++;
- }
- return decodedMessage.join("");
- }
- function translater(){
- response =
- encodeMessage(document.forms["mainForm"]["input"].value,key);
- }
- document.forms["mainForm"]["output"].value = response;
- } else {
- var key = getKey();
- var response = "";
- if (key === []){
- response = "Invalid input for cipher key";
- } else{
- response =
- decodeMessage(document.forms["mainForm"]["input"].value,key);
- if(response === ""){
- response = "Invalid input for decoding";
- }
- }
- document.forms["mainForm"]["output"].value = response;
- }
- } </script>
- </head>
- <body> <h1>
- Poly Cipher
- </h1>
- <form name = "mainForm" autocomplete = "off">
- Input the key for translating the message(Put spaces between
- the numbers): <input type = "text" name = "key" />
- <br/>
- <br/>
- <hr>
- <br/>
- Input a message to translate:
- <br/>
- <textarea rows = 5 cols = 50 name = "input"></textarea>
- <br/>
- <br/>
- Translated message:
- <br/>
- <textarea rows = 5 cols = 50 name = "output" readonly>
- </textarea>
- <br/>
- <input type = "radio" name = "translation" value = "encode"
- checked> Encode Message
- <input type = "radio" name = "translation" value = "decode">
- Decode Message
- <br/>
- <br/>
- <input type = "button" onclick = "translater();" name =
- "btnTranslate" value = "Translate" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement