Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created December 27, 2016 04:36
Show Gist options
  • Save railsstudent/c654c7e5b24cba8957ef9cd89c91db95 to your computer and use it in GitHub Desktop.
Save railsstudent/c654c7e5b24cba8957ef9cd89c91db95 to your computer and use it in GitHub Desktop.
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