Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created September 26, 2015 20:32
Show Gist options
  • Save Lazhari/82d1aa6ef998096a2417 to your computer and use it in GitHub Desktop.
Save Lazhari/82d1aa6ef998096a2417 to your computer and use it in GitHub Desktop.
Example Node.js 4.0.0/ES6 : Vigenère cipher Class
'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