Skip to content

Instantly share code, notes, and snippets.

@sid24rane
Created May 14, 2016 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sid24rane/a92a8ab040b61f7273f39b0bef48db81 to your computer and use it in GitHub Desktop.
Save sid24rane/a92a8ab040b61f7273f39b0bef48db81 to your computer and use it in GitHub Desktop.
Simple Implementation of Ceasar cipher in JS (In 5min)
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