Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Created November 24, 2010 10:53
Show Gist options
  • Save Majkl578/713480 to your computer and use it in GitHub Desktop.
Save Majkl578/713480 to your computer and use it in GitHub Desktop.
Příklady: ruby main.rb encrypt input.txt output.txt keys.txt, ruby main.rb decrypt output.txt result.txt keys.txt
class Encryptor
def self.encrypt(string, raw_key)
key = normalize_key(raw_key, string.length)
buf = []
string.length.times do |i|
char_code = (string[i] + key[i])
char_code -= 256 while char_code > 255
buf[i] = char_code.chr
end
return buf.to_s
end
def self.decrypt(string, raw_key)
key = normalize_key(raw_key, string.length)
buf = []
string.length.times do |i|
char_code = (string[i] - key[i])
char_code += 256 while char_code < 0
buf[i] = char_code.chr
end
return buf.to_s
end
private
def self.normalize_key(key, length)
key * (length / key.length) + key[0, length % key.length]
end
end
Frantisku, uz te nepotesi,
2
ze Francouz jsi a ze vsi zdejsi;
1
ted na krk opratku ti vesi;
4
at pozna, oc je zadek tezsi.
2
jednorozec
Unicorn
Meda Beda
nbusr123
heslo_je_veslo
require 'Encryptor'
raise '4 parameters expected: mode input_file output_file keys_file' if ARGV.count != 4
mode = ARGV[0]
input_file = ARGV[1]
output_file = ARGV[2]
keys_file = ARGV[3]
raise 'Invalid mode, one of encrypt/decrypt expected' if !['encrypt', 'decrypt'].include?(mode)
raise 'Input file not found' if !File.exist?(input_file)
raise 'Keys file not found' if !File.exist?(keys_file)
keys = []
File.open(keys_file, 'r') do |file|
i = 1
file.each_line do |line|
keys[i] = line
i += 1
end
end
File.open(input_file, 'r') do |file_i|
File.open(output_file, 'w') do |file_o|
buffer = []
file_i.each_line do |line|
buffer << line
end
for i in ((0..(buffer.size / 2 - 1)).map {|j| j*2})
key = keys[buffer[i+1].to_i]
if (key.nil?)
puts 'Nelze ' + (mode == 'encrypt' ? 'zasifrovat' : 'desifrovat') + ' zpravu "' + buffer[i] + '", protoze poradi klice (' + buffer[i+1] + ') neni platne'
next
end
result = mode == 'encrypt' ? Encryptor::encrypt(buffer[i].strip, key.strip) : Encryptor::decrypt(buffer[i].strip, key.strip)
file_o.puts result
file_o.puts buffer[i+1]
end
end
end
@Majkl578
Copy link
Author

No jo, ruby… To je tak když ho máme ve škole, jinak bych se asi neodvážil. :P

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