Created
October 9, 2018 16:28
-
-
Save 64lines/33050df0a6648ee2fff6bce804862638 to your computer and use it in GitHub Desktop.
[JAVASCRIPT] Caesar Encryption
This file contains hidden or 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
| var encrypt = function(plaintext, shiftAmount) { | |
| var ciphertext = ""; | |
| for(var i = 0; i < plaintext.length; i++) { | |
| var plainCharacter = plaintext.charCodeAt(i); | |
| if(plainCharacter >= 97 && plainCharacter <= 122) { | |
| ciphertext += String.fromCharCode((plainCharacter - 97 + shiftAmount) % 26 + 97); | |
| } else if(plainCharacter >= 65 && plainCharacter <= 90) { | |
| ciphertext += String.fromCharCode((plainCharacter - 65 + shiftAmount) % 26 + 65); | |
| } else { | |
| ciphertext += String.fromCharCode(plainCharacter); | |
| } | |
| } | |
| return ciphertext; | |
| } | |
| var decrypt = function(ciphertext, shiftAmount) { | |
| var plaintext = ""; | |
| for(var i = 0; i < ciphertext.length; i++) { | |
| var cipherCharacter = ciphertext.charCodeAt(i); | |
| if(cipherCharacter >= 97 && cipherCharacter <= 122) { | |
| plaintext += String.fromCharCode((cipherCharacter - 97 - shiftAmount + 26) % 26 + 97); | |
| } else if(cipherCharacter >= 65 && cipherCharacter <= 90) { | |
| plaintext += String.fromCharCode((cipherCharacter - 65 - shiftAmount + 26) % 26 + 65); | |
| } else { | |
| plaintext += String.fromCharCode(cipherCharacter); | |
| } | |
| } | |
| return plaintext; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment