19 lines
576 B
Elixir
19 lines
576 B
Elixir
defmodule Caesar_cipher do
|
|
defp set_map(map, range, key) do
|
|
org = Enum.map(range, &List.to_string [&1])
|
|
{a, b} = Enum.split(org, key)
|
|
Enum.zip(org, b ++ a) |> Enum.into(map)
|
|
end
|
|
|
|
def encode(text, key) do
|
|
map = Map.new |> set_map(?a..?z, key) |> set_map(?A..?Z, key)
|
|
String.graphemes(text) |> Enum.map_join(fn c -> Map.get(map, c, c) end)
|
|
end
|
|
end
|
|
|
|
text = "The five boxing wizards jump quickly"
|
|
key = 3
|
|
IO.puts "Original: #{text}"
|
|
IO.puts "Encrypted: #{enc = Caesar_cipher.encode(text, key)}"
|
|
IO.puts "Decrypted: #{Caesar_cipher.encode(enc, -key)}"
|