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 = "AGUNG FHAJAR FADILAH IF-B CIANJUR" key = 5 IO.puts "ASSALAMUALAIKUM WR.WB" IO.puts "Original: #{text}" IO.puts "Encrypted: #{enc = Caesar_cipher.encode(text, key)}" IO.puts "Decrypted: #{Caesar_cipher.encode(enc, -key)}"
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more