Created
September 26, 2015 20:32
-
-
Save Lazhari/82d1aa6ef998096a2417 to your computer and use it in GitHub Desktop.
Example Node.js 4.0.0/ES6 : Vigenère cipher Class
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
'use strict'; | |
class VigenereCipher { | |
constructor(key) { | |
this.key = key; | |
} | |
crypt(text) { | |
var cypher = ''; | |
text = text.toUpperCase(); | |
for(let i =0; i<text.length; i++) { | |
if(text[i] === " ") { | |
cypher += text[i]; | |
} else { | |
cypher += String.fromCharCode(((text.charCodeAt(i) + this.key.charCodeAt(i))%26)+65) | |
} | |
} | |
return cypher; | |
}; | |
decrypt(cypher) { | |
var text = ''; | |
for(let i =0; i<cypher.length; i++) { | |
if(cypher[i] === " ") { | |
text += cypher[i]; | |
} else { | |
text += String.fromCharCode(((((cypher.charCodeAt(i) - this.key.charCodeAt(i)) % 26)+26)%26)+65) | |
} | |
} | |
return text; | |
} | |
} | |
var vCrypt = new VigenereCipher('M USIQU EMUSIQU EM USIQU EMUSI QU EMUSIQU'); | |
console.log(vCrypt.crypt('j adore ecouter la radio toute la journee')); | |
console.log(vCrypt.decrypt('V UVWHY IOIMBUL PM LSLYI XAOLM BU NAOJVUY')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment