Avatar of Sendell

Sendell's solution

to Atbash Cipher in the OCaml Track

Published at Jul 21 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.


  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

Deprecation of String.lowercase

Depending on the version of OCaml you installed the use of String.lowercase is frowned upon. Since version 4.03.0 String.lowercase is deprecated in favor of String.lowercase_ascii. So instead of writing

String.lowercase "Hello, World!"


String.lowercase_ascii "Hello, World!"

See String documentation for more useful functions.

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:


Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!


Wikipedia http://en.wikipedia.org/wiki/Atbash


open OUnit2
open Atbash_cipher

let ae exp got _test_ctxt = assert_equal ~printer:(fun x -> x) exp got

let encode_tests = [
   "encode yes" >::
     ae "bvh" (encode "yes");
   "encode no" >::
     ae "ml" (encode "no");
   "encode OMG" >::
     ae "lnt" (encode "OMG");
   "encode spaces" >::
     ae "lnt" (encode "O M G");
   "encode mindblowingly" >::
     ae "nrmwy oldrm tob" (encode "mindblowingly");
   "encode numbers" >::
     ae "gvhgr mt123 gvhgr mt" (encode "Testing,1 2 3, testing.");
   "encode deep thought" >::
     ae "gifgs rhurx grlm" (encode "Truth is fiction.");
   "encode all the letters" >::
     ae "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" (encode "The quick brown fox jumps over the lazy dog.");

let decode_tests = [
   "decode exercism" >::
     ae "exercism" (decode "vcvix rhn");
   "decode a sentence" >::
     ae "anobstacleisoftenasteppingstone" (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v");
   "decode numbers" >::
     ae "testing123testing" (decode "gvhgr mt123 gvhgr mt");
   "decode all the letters" >::
     ae "thequickbrownfoxjumpsoverthelazydog" (decode "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
   "decode with too many spaces" >::
     ae "exercism" (decode "vc vix    r hn");
   "decode with no spaces" >::
     ae "anobstacleisoftenasteppingstone" (decode "zmlyhgzxovrhlugvmzhgvkkrmthglmv");

let different_block_size_test = [
  "encode mindblowingly with a different block size" >::
    ae "n r m w y o l d r m t o b" (encode ~block_size:1 "mindblowingly");

let () =
  run_test_tt_main (
    "atbash-cipher tests" >:::
      List.concat [encode_tests; decode_tests; different_block_size_test]


(* Encoding from English to atbash cipher. The default for block_size is 5 *)
val encode : ?block_size:int -> string -> string

(* Decoding from atbash to English *)
val decode : string -> string


open Base

let atbash ?(length = 26) x = (((length - 1) * x) + (length - 1)) % length

let map_atbash c =
  let offset = Char.to_int 'a' in
  if Char.is_alpha c then
    Char.to_int c - offset |> atbash |> ( + ) offset |> Char.of_int_exn
  else c

let lower_alpha_num str =
  String.filter ~f:Char.is_alphanum str |> String.map ~f:Char.lowercase

let encode ?(block_size = 5) str =
  lower_alpha_num str |> String.to_list |> List.map ~f:map_atbash
  |> List.groupi ~break:(fun i _ _ -> i % block_size = 0)
  |> List.map ~f:String.of_char_list
  |> String.concat ~sep:" "

let decode str = lower_alpha_num str |> String.map ~f:map_atbash

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?