Skip to content

Instantly share code, notes, and snippets.

@CoderPuppy
Created November 25, 2014 02:13
Show Gist options
  • Select an option

  • Save CoderPuppy/565ffb2dfb2eefdcb07e to your computer and use it in GitHub Desktop.

Select an option

Save CoderPuppy/565ffb2dfb2eefdcb07e to your computer and use it in GitHub Desktop.
Modified caesar shift
Alphabet = Struct.new :chars do
def [] v
case v
when Fixnum
chars[v]
when String
chars.index v
end
end
end
Alphabet::UTF8 = Alphabet.new (0...256).map(&:chr).join("")
alphabet = Alphabet.new "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.!?,;'\"()- \n"
def encode str, alphabet: Alphabet::UTF8, fixed_key: 0, variable_key: 0
str
.split("")
.map(&alphabet.method(:[]))
.each_with_index
.map { |v, i| v + fixed_key + (i * variable_key) }
.join(",")
end
def decode str, alphabet: Alphabet::UTF8, fixed_key: 0, variable_key: 0
str
.split(",")
.map(&:to_i)
.each_with_index
.map { |v, i| v - fixed_key - (i * variable_key) }
.map(&alphabet.method(:[]))
.join("")
end
fixed_key = 0
variable_key = 1
encoded = encode("The quick brown fox jumped over the lazy dog.", fixed_key: fixed_key, variable_key: variable_key, alphabet: alphabet)
ap encoded
ap decode(encoded, fixed_key: fixed_key, variable_key: variable_key, alphabet: alphabet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment