Created
January 30, 2014 07:59
-
-
Save jameslyons/8704344 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// enciphering | |
plaintext = "DEFEND THE EAST WALL OF THE CASTLE"; | |
var key = 3; | |
ciphertext = ""; var re = /[a-z]/; | |
for(i=0; i<plaintext.length; i++){ | |
if(re.test(plaintext.charAt(i))) ciphertext += String.fromCharCode((plaintext.charCodeAt(i) - 97 + key)%26 + 97); | |
else ciphertext += plaintext.charAt(i); | |
} | |
// deciphering | |
plaintext2 = ""; | |
for(i=0; i<ciphertext.length; i++){ | |
if(re.test(ciphertext.charAt(i))) plaintext2 += String.fromCharCode((ciphertext.charCodeAt(i) - 97 + 26 - key)%26 + 97); | |
else plaintext += ciphertext.charAt(i); | |
} | |
alert(plaintext) | |
alert(ciphertext) | |
alert(plaintext2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment