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
Tell me more
×
Cryptography Stack Exchange is a question and answer site for
software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.
|
|
|||||||||||
|
|
This appears to be a simple substitution cipher. Substitution ciphers are vulnerable to frequency analysis, and would never be used in practice. It would be very easy to guess what character is being used to encrypt underscores (spaces), and from there an attacker could guess common one-, two-, and three-letter words and begin unraveling your messages from there. See the relevant section of the Wikipedia article for more information. |
|||
|
|