项目作者: TeWu

项目描述 :
Implementation of Caesar and Vigenere ciphers
高级语言: Ruby
项目地址: git://github.com/TeWu/shift-ciphers.git
创建时间: 2018-10-06T11:16:00Z
项目社区:https://github.com/TeWu/shift-ciphers

开源协议:MIT License

下载


Shift Ciphers Gem Version Build Status

Shift Ciphers gem is simple, yet complete, implementation of classic Caesar and Vigenère ciphers. It also features custom, hardened version of Vigenère cipher, which uses autokey scheme and PRNGs.

Installation

  1. gem install shift_ciphers

Basic usage

  1. require 'shift_ciphers'
  2. plaintext = "Attack at dawn!"
  3. encrypted = ShiftCiphers::Caesar.encrypt(plaintext, offset: 5) # => "Fyyfhp%fy%ifBs^"
  4. decrypted = ShiftCiphers::Caesar.decrypt(encrypted, offset: 5) # => "Attack at dawn!"
  5. decrypted == plaintext # Should be true
  6. encrypted = ShiftCiphers::Vigenere.encrypt(plaintext, "my keyword") # => "W!0uqS3yU=zI3H{"
  7. decrypted = ShiftCiphers::Vigenere.decrypt(encrypted, "my keyword") # => "Attack at dawn!"
  8. decrypted == plaintext # Should be true
  9. encrypted = ShiftCiphers::HardenedVigenere.encrypt(plaintext, "my keyword") # => "$Uj:o 2M9S+<Cq9"
  10. decrypted = ShiftCiphers::HardenedVigenere.decrypt(encrypted, "my keyword") # => "Attack at dawn!"
  11. decrypted == plaintext # Should be true

… or instantiate a cipher, and benefit from stored configuration info (e.g. offset for Caesar cipher, or key for Vigenère):

  1. caesar = ShiftCiphers::Caesar.new
  2. caesar.offset = 5
  3. encrypted = caesar.encrypt(plaintext) # => "Fyyfhp%fy%ifBs^"
  4. decrypted = caesar.decrypt(encrypted) # => "Attack at dawn!"
  5. decrypted == plaintext # Should be true
  6. vigenere = ShiftCiphers::Vigenere.new("my keyword")
  7. encrypted = vigenere.encrypt(plaintext) # => "W!0uqS3yU=zI3H{"
  8. decrypted = vigenere.decrypt(encrypted) # => "Attack at dawn!"
  9. decrypted == plaintext # Should be true
  10. strong_vigenere = ShiftCiphers::HardenedVigenere.new("my keyword")
  11. encrypted = strong_vigenere.encrypt(plaintext) # => "$Uj:o 2M9S+<Cq9"
  12. decrypted = strong_vigenere.decrypt(encrypted) # => "Attack at dawn!"
  13. decrypted == plaintext # Should be true

You can customize alphabet used by cipher:

  1. plaintext = "ATTACKATDAWN"
  2. encrypted = ShiftCiphers::Vigenere.encrypt(plaintext, "KEYWORD", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ") # => "KXRWQBDDHYSB"
  3. decrypted = ShiftCiphers::Vigenere.decrypt(encrypted, "KEYWORD", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ") # => "ATTACKATDAWN"
  4. decrypted == plaintext # Should be true

When you attempt to encrypt a string, which contains character that is not in the cipher’s alphabet, then ShiftCiphers::CipherError is rised:

  1. plaintext = "ATTACK!"
  2. encrypted = ShiftCiphers::Vigenere.encrypt(plaintext, "KEYWORD", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  3. # Raises ShiftCiphers::CipherError: Invalid input "ATTACK!". Character "!" is not in the alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

You can avoid this exception by telling cipher not to encrypt characters which are not in its alphabet. This is done by passing nonalphabet_char_strategy argument to encrypt/decrypt class method (or by using nonalphabet_char_strategy= instance method):

  1. plaintext = "ATTACK AT DAWN!"
  2. encrypted = ShiftCiphers::Vigenere.encrypt(plaintext, "KEYWORD", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", nonalphabet_char_strategy: :dont_encrypt)
  3. decrypted = ShiftCiphers::Vigenere.decrypt(encrypted, "KEYWORD", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", nonalphabet_char_strategy: :dont_encrypt)
  4. puts plaintext # => ATTACK AT DAWN!
  5. puts encrypted # => KXRWQB DD HYSB!
  6. decrypted == plaintext # Should be true