Skip to content

Instantly share code, notes, and snippets.

@hackervera
Created September 3, 2011 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackervera/1191944 to your computer and use it in GitHub Desktop.
Save hackervera/1191944 to your computer and use it in GitHub Desktop.
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
@hackervera
Copy link
Author

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=>_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment