Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created December 27, 2016 05:56
Show Gist options
  • Save railsstudent/8e29fdd5adc27ac6f300be085f0f1ce3 to your computer and use it in GitHub Desktop.
Save railsstudent/8e29fdd5adc27ac6f300be085f0f1ce3 to your computer and use it in GitHub Desktop.
function VigenèreCipher(key, abc) {
this.encode = function (str) {
var encodeStr = ''
for (var i in str) {
var c = str[i];
var k = key[i % key.length];
if (abc.indexOf(c) >= 0) {
encodeStr += abc[(abc.indexOf(c) + abc.indexOf(k)) % abc.length];
} else {
encodeStr += c;
}
}
return encodeStr;
};
this.decode = function (str) {
var decodeStr = '';
for (var i in str) {
var z = str[i];
var k = key[i % key.length];
if (abc.indexOf(z) >= 0) {
var idx = (abc.indexOf(z) + abc.length - abc.indexOf(k)) % abc.length;
var c = abc[idx];
decodeStr += c;
} else {
decodeStr += z;
};
}
return decodeStr
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment