Created
November 24, 2010 10:53
-
-
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
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
jednorozec | |
Unicorn | |
Meda Beda | |
nbusr123 | |
heslo_je_veslo |
This file contains hidden or 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
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 |
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
Není možné, ruby! Peklo zamrlo? :)