// Implement modulo by replacing the negative operand // with an equivalent positive operand that has the same wrap-around effect function mod(n, p) { if (n < 0) n = p - Math.abs(n) % p; return n % p; } // Function will implement Caesar Cipher to // encrypt / decrypt the msg by shifting the letters // of the message acording to the key function encrypt(msg, key) { var encMsg = ""; key -= 0; for (var i = 0; i < msg.length; i++) { var code = msg.charCodeAt(i); // Encrypt only letters in 'A' ... 'Z' interval if (code >= 97 && code <= 97 + 26 - 1) { code -= 97; code = mod(code + key, 26); code += 97; } if (code >= 65 && code <= 65 + 26 - 1) { code -= 65; code = mod(code + key, 26); code += 65; } encMsg += String.fromCharCode(code); } return encMsg; } function decrypt(msg, key) { return encrypt(msg, 26 - key); }