Created
September 3, 2011 23:32
-
-
Save hackervera/1191944 to your computer and use it in GitHub Desktop.
Encryption Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Output