Sub(stitution) Cipher

Gem Version Build Status Coverage Status Inline docs

Encode/Decode text with the substitution cipher


$ gem install sub_cipher


Use default seeds(alphabets) to generate the mapping, note that the letters with different cases would be mapped to the same letter, for example: if "a" is mapped to "b", then "A" is mapped to "B".

sc = SubCipher.gen
sc.encode("Here is a secret.")
# "Wyky gn q nyakyr."
sc.decode("Wyky gn q nyakyr.")
# "Here is a secret."
# "abcdefghijklmnopqrstuvwxyz"
# "qeahyftwgpjixodzbknrlscvum"

The seed and map method shows how to map the cipher, the above example means mapping "abcdefghijklmnopqrstuvwxyz" to "qeahyftwgpjixodzbknrlscvum"



Use :s (or :seed) to map the given seeds only

sc = SubCipher.gen(seed: "abcde")
sc.encode("Here is a secret.")
# "Hcrc is e scbrct."
sc.decode("Hcrc is e scbrct.")
# "Here is a secret."
# "abcde"
# "edbac"


Use :m (or :map) option to initalize cipher with a map. Note that the :seed option would be skipped if both :seed and :map options are given.

sc = SubCipher.gen(seed: "bdeac")
sc.encode("Here is a secret.")
# "Hara is b saerat."
sc.decode("Hara is b saerat.")
# "Here is a secret."
# "abcde"
# "bdeca"

Keep Case

If you want to map letters with different cases to different letters, use k: false (or keep_case: false) option.

sc = SubCipher.gen(keep_case: false)
sc.encode("Here is a secret.")
# "alXl wE s EleXlk."
sc.decode("alXl wE s EleXlk.")
# "Here is a secret."
# "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# "PNAgWKOaxtCUdqHpIuJRhjTMnDsbeQlFiGwrzLfBvVYXEkZcoSmy"

If there is a seed(assigned by seed or map option) which is not an alphabet, keep_case: false would be applied.

sc = SubCipher.gen(seed: "abcdeABCDE ,.")
sc.encode("Here is a secret.")
# "HdrdAisA,AsdBrdtE"
# "Here is a secret."
# " ,.ABCDEabcde"
# "AaE ebCc,DB.d"


Go to gem folder and run

ruby ./test/test_all.rb

(Note that you need minitest ~> 5.0 to run these tests)


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write tests for your code
  4. Commit your changes (both code and tests) (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request


Sibevin Wang

Copyright (c) 2014 Sibevin Wang. Released under the MIT license.