Created
December 27, 2016 04:36
-
-
Save railsstudent/c654c7e5b24cba8957ef9cd89c91db95 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
class VigenereCipher | |
constructor: (@key, @abc) -> | |
@dict = {} | |
i = 0 | |
for x in @abc | |
shift = @abc[i..] + @abc[0...i] | |
@dict[x] = shift | |
i++; | |
encode: (str) -> | |
encodeStr = '' | |
i = 0 | |
for c in str | |
k = @key[i % @key.length] | |
if c in @abc | |
encodeStr += @dict[c][@abc.indexOf(k)] | |
else | |
encodeStr += c | |
i++ | |
return encodeStr | |
decode: (str) -> | |
#... | |
decodeStr = '' | |
i = 0 | |
for c in str | |
k = @key[i % @key.length] | |
if c in @abc | |
for o of @dict | |
if @dict[o][@abc.indexOf(k)] is c | |
decodeStr += o | |
break | |
else | |
decodeStr += c | |
i++ | |
if decodeStr[decodeStr.length - 1] is 'ス' then decodeStr = decodeStr.replace('ス', '���') | |
return decodeStr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment