Skip to content

Instantly share code, notes, and snippets.

@JonahMoses
Created September 13, 2013 04:55
Show Gist options
  • Save JonahMoses/6546889 to your computer and use it in GitHub Desktop.
Save JonahMoses/6546889 to your computer and use it in GitHub Desktop.
puts "Enter Password: "
password = gets
if password.chomp != "password"
abort("WRONG PASSWORD")
end
class Encryptor
def cipher(rotation)
characters = (' '..'z').to_a
rotated_characters = characters.rotate(rotation)
Hash[characters.zip(rotated_characters)]
end
def encrypt_letter(letter,rotation)
cipher_for_rotation = cipher(rotation)
cipher_for_rotation[letter]
end
def encrypt(string,rotation)
letters = string.split("")
encrypted_letter = letters.collect do |letter|
encrypted_letter = encrypt_letter(letter,rotation)
end
encrypted_letter.join
end
def decrypt_letter(letter,rotation)
cipher_for_rotation = cipher(rotation)
cipher_for_rotation[letter]
end
def decrypt(string,rotation)
letters = string.split("")
decrypted_letter = letters.collect do |letter|
decrypted_letter = decrypt_letter(letter,-rotation)
end
decrypted_letter.join
end
def encrypt_file(filename, rotation)
input = File.open(filename, "r")
read_file = input.read
encrypted_file = encrypt(read_file, rotation)
encrypted_file_name = filename+".encrypted"
out = File.open(encrypted_file_name, "w")
out.write(encrypted_file)
out.close
end
def decrypt_file(filename, rotation)
input = File.open(filename, "r")
read_file = input.read
decrypted_file = decrypt(read_file, rotation)
decrypted_file_name = filename.gsub("encrypted", "decrypted")
out = File.open(decrypted_file_name, "w")
out.write(decrypted_file)
out.close
end
def supported_characters
(' '..'z').to_a
end
def crack(message)
supported_characters.count.times.collect do |attempt|
decrypt(message,attempt)
end
end
def e_chat(rotation)
puts "Begin Real-Time Encryption Chat\n"
begin
puts "Input: "
msg = gets
e_msg = encrypt(msg,rotation)
puts "Encrypted Message: #{e_msg}"
end until msg.chomp.eql? "Quit"
end
def d_chat(rotation)
puts "Begin Real-Time Decryption Chat\n"
begin
puts "Input: "
msg = gets
d_msg = decrypt(msg,rotation)
puts "Decrypted Message: #{d_msg}"
end until msg.chomp.eql? "Quit"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment