Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <form action="#" method="get" onsubmit="return false;" _lpchecked="1">
- <label for="text">Text:</label>
- <textarea id="text" cols="50" rows="10">THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.</textarea>
- <br>
- <label for="shift">Shift:</label>
- <input type="text" id="shift" value="13" />
- <br>
- <input type="button" value="Encrypt" onclick="doCrypt(false);" />
- <input type="button" value="Decrypt" onclick="doCrypt(true);" />
- </form>
- <script>
- function doCrypt(isDecrypt) { // this func figures out the shift value, and also the text
- var shift = parseInt(document.getElementById("shift").value); // find how many chars to shift by.
- //var shift = shiftText, 10; // parsing the number from the text, is now done by the prev line
- if (shift < 0 || shift >= 26) { //make sure shift from 0-26
- alert("Shift is out of range");
- return; //break the function
- }
- if (isDecrypt) // if its already decrypted
- shift = (26 - shift) % 26; // figure out what the shift was.
- var textElem = document.getElementById("text"); // figure out what the text actually is.
- textElem.value = caesarShift(textElem.value, shift); // un-decrypt it.
- }
- function caesarShift(text, shift) {
- var result = ""; // used to display the en/de crypted text
- for (var i = 0; i < text.length; i++) { // a for loop to make sure that the text is fully changed.
- var c = text.charCodeAt(i); // used to determine what the character is according to the computer
- if (c >= 65 && c <= 90) result += String.fromCharCode((c - 65 + shift) % 26 + 65); // Uppercase
- else if (c >= 97 && c <= 122) result += String.fromCharCode((c - 97 + shift) % 26 + 97); // Lowercase
- else result += text.charAt(i); // Copy
- }
- return result; // display the result in the text box that text was entered into
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment