Project

sub_cipher

0.0
No commit activity in last 3 years
No release in over 3 years
Encode/Decode text with substitution cipher, please see "README" to get more details.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.0
 Project Readme

Sub(stitution) Cipher

Gem Version Build Status Coverage Status Inline docs

Encode/Decode text with the substitution cipher

Installation

$ gem install sub_cipher

Usage

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."
sc.seed
# "abcdefghijklmnopqrstuvwxyz"
sc.map
# "qeahyftwgpjixodzbknrlscvum"

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

Options

Seed

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."
sc.seed
# "abcde"
sc.map
# "edbac"

Map

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."
sc.seed
# "abcde"
sc.map
# "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."
sc.seed
# "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
sc.map
# "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"
sc.decode("HdrdAisA,AsdBrdtE")
# "Here is a secret."
sc.seed
# " ,.ABCDEabcde"
sc.map
# "AaE ebCc,DB.d"

Test

Go to gem folder and run

ruby ./test/test_all.rb

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

Contributing

  1. Fork it ( https://github.com/sibevin/sub_cipher/fork )
  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

Authors

Sibevin Wang

Copyright

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