Instantly share code, notes, and snippets.

# tjgillies/crypto.rb Created Sep 3, 2011

What would you like to do?
Encryption Library
 module SimpleCrypt class Map attr_accessor :letters def initialize @letters = ('a'..'z').to_a @letters << "_" @letters.shuffle! end end class Cipher def initialize(map) @letters = map.letters end def mapping(type, character) matches(@letters, type, character) end def matches(letters, type, character) letters_copy = letters.dup encrypt = Proc.new{|letter| "#{letter}#{character}#{letters_copy.shift}" } decrypt = Proc.new{|letter| "#{letters_copy.shift}#{character}#{letter}" } if type == "encrypt" encrypt_map = ('a'..'z').to_a.map{ |letter| encrypt[letter] } encrypt_map << "_#{character}#{letters_copy.shift}" else encrypt_map = ('a'..'z').to_a.map{ |letter| decrypt[letter] }.sort encrypt_map << "#{letters_copy.shift}#{character}_" end end end class Encrypter < Cipher def mapping super('encrypt', @character) end def initialize(map, character = "=>") super(map) @character = character end end class Decrypter < Cipher def mapping super('decrypt', @character) end def initialize(map, character = "=>") super(map) @character = character end end end map = SimpleCrypt::Map.new encrypter = SimpleCrypt::Encrypter.new(map) puts encrypter.mapping puts "" decrypter = SimpleCrypt::Decrypter.new(map) puts decrypter.mapping
Owner

### tjgillies commented Sep 3, 2011

 Example Output ``````a=>p b=>k c=>z d=>w e=>o f=>q g=>s h=>a i=>j j=>c k=>l l=>u m=>d n=>x o=>b p=>m q=>r r=>e s=>t t=>g u=>y v=>_ w=>f x=>v y=>i z=>n _=>h _=>v a=>h b=>o c=>j d=>m e=>r f=>w g=>t i=>y j=>i k=>b l=>k m=>p n=>z o=>e p=>a q=>f r=>q s=>g t=>s u=>l v=>x w=>d x=>n y=>u z=>c h=>_ ``````