Created
December 27, 2016 05:56
-
-
Save railsstudent/8e29fdd5adc27ac6f300be085f0f1ce3 to your computer and use it in GitHub Desktop.
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 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