Skip to content

Instantly share code, notes, and snippets.

@jameslyons
Created January 30, 2014 07:59
Show Gist options
  • Save jameslyons/8704344 to your computer and use it in GitHub Desktop.
Save jameslyons/8704344 to your computer and use it in GitHub Desktop.
// 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