Skip to content

Instantly share code, notes, and snippets.

@leonmwandiringa
Created November 1, 2018 09:18
Show Gist options
  • Save leonmwandiringa/f109a1a4cc3a9affa93f0376a834f33f to your computer and use it in GitHub Desktop.
Save leonmwandiringa/f109a1a4cc3a9affa93f0376a834f33f to your computer and use it in GitHub Desktop.
/*
* Handles the HTML input/output for Caesar cipher encryption/decryption.
* This is the one and only entry point function called from the HTML code.
*/
function doCrypt(isDecrypt) {
var shiftText = document.getElementById("shift").value;
if (!/^-?\d+$/.test(shiftText)) {
alert("Shift is not an integer");
return;
}
var shift = parseInt(shiftText, 10);
if (shift < 0 || shift >= 26) {
alert("Shift is out of range");
return;
}
if (isDecrypt)
shift = (26 - shift) % 26;
var textElem = document.getElementById("text");
textElem.value = caesarShift(textElem.value, shift);
}
/*
* Returns the result of having each alphabetic letter of the given text string shifted forward
* by the given amount, with wraparound. Case is preserved, and non-letters are unchanged.
* Examples:
* - caesarShift("abz", 0) = "abz".
* - caesarShift("abz", 1) = "bca".
* - caesarShift("abz", 25) = "zay".
* - caesarShift("THe 123 !@#$", 13) = "GUr 123 !@#$".
*/
function caesarShift(text, shift) {
var result = "";
for (var i = 0; i < text.length; i++) {
var c = text.charCodeAt(i);
if (65 <= c && c <= 90) result += String.fromCharCode((c - 65 + shift) % 26 + 65); // Uppercase
else if (97 <= c && c <= 122) result += String.fromCharCode((c - 97 + shift) % 26 + 97); // Lowercase
else result += text.charAt(i); // Copy
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment