Created
May 14, 2016 15:23
-
-
Save sid24rane/a92a8ab040b61f7273f39b0bef48db81 to your computer and use it in GitHub Desktop.
Simple Implementation of Ceasar cipher in JS (In 5min)
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
function ceaser_cipher(){ | |
this.alphabets=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; | |
} | |
ceaser_cipher.prototype.encrypt=function(s,n){ | |
var plain_text=[...s]; | |
for(var i = 0, length1 = plain_text.length; i < length1; i++){ | |
plain_text[i]=this.alphabets[this.alphabets.indexOf(plain_text[i])+n]; | |
} | |
return plain_text.toString(); | |
}; | |
ceaser_cipher.prototype.decrypt=function(s,n){ | |
var cipher_text = [...s]; | |
for(var i = 0, length1 = cipher_text.length; i < length1; i++){ | |
cipher_text[i]=this.alphabets[this.alphabets.indexOf(cipher_text[i])-n]; | |
} | |
return cipher_text.toString(); | |
}; | |
var secret_msg = new ceaser_cipher(); | |
var cipher_text = secret_msg.encrypt("abc",3); | |
var plain_text = secret_msg.decrypt("def",3); | |
console.log('The encoded one : ' + cipher_text); | |
console.log('The decoded one : ' + plain_text); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment