forked from robertsosinski/ruby-des
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.rb
executable file
·152 lines (119 loc) · 3.26 KB
/
run.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#encoding: utf-8
require './ruby-des'
@key = RubyDES::Block.new("kerouac!")
def set_key(block)
@key = RubyDES::Block.new(block)
end
def get_key
@key
end
@output_encrypted = "encrypted.xxx"
@output_decrypted = "decrypted.txt"
def set_output(target, filename)
if target == :encrypt
@output_encrypted = filename
elsif target == :decrypt
@output_decrypted = filename
else
raise "Wrong target"
end
end
def get_output(target)
if target == :encrypt
@output_encrypted
elsif target == :decrypt
@output_decrypted
else
raise "Wrong target"
end
end
def encrypt_block(block)
block.flatten!
tmp_block = RubyDES::Block.new(block)
encrypted = RubyDES::Ctx.new(tmp_block, get_key).encrypt
File.open(get_output(:encrypt), 'a+').write(encrypted.string)
end
def decrypt_block(block)
block.flatten!
tmp_block = RubyDES::Block.new(block)
decrypted = RubyDES::Ctx.new(tmp_block, get_key).decrypt
File.open(get_output(:decrypt), 'a+').write(decrypted.string)
end
def do_file( options = { filename: '', action: '' } )
raise "No filename given" if options[:filename].length.zero?
preset_actions = [:read_key, :encrypt, :decrypt]
action = (preset_actions & [options[:action]]).size.zero? ? '' : options[:action]
raise "Wrong action" if action.length.zero?
current_block = []
bytes_count = 0
File.open(options[:filename], 'r') do |input_file|
input_file.each_byte do |byte|
byte = byte.to_s(2).rjust(8, '0').split('').map(&:to_i)
if bytes_count < 8
current_block << byte
bytes_count += 1
else
case action
when :read_key
set_key(current_block)
when :encrypt
encrypt_block(current_block)
when :decrypt
decrypt_block(current_block)
end
bytes_count = 1
current_block = []
current_block << byte
end
end
byte_size_diff = 8 - current_block.size
byte_size_diff.times do
current_block << [0,0,0,0,0,0,0,0]
end
case action
when :read_key
set_key(current_block)
when :encrypt
encrypt_block(current_block)
when :decrypt
decrypt_block(current_block)
end
end
end
def read_keyfile(filename)
do_file(filename: filename, action: set_key)
end
def run_interface
puts "\n"*5
puts "############################"
puts "Ruby DES encryptor/decryptor"
puts "############################"
puts "\nremember that DES is deprecated"
puts "\n"*2
puts "Availiable actions\n1 - encrypt\n2 - decrypt"
puts "\n"
print "Choise: "
action = gets.to_i
if action == 1
action = :encrypt
elsif action == 2
action = :decrypt
else
puts "Wrong number!"
return
end
print "Enter filename of source: "
source = gets.chomp
if source.length.zero?
puts "Empty filename!"
return
end
print "Enter filename of key or blank for default: "
key_filename = gets.chomp
set_key read_keyfile(key_filename) unless key_filename.length.zero?
print "Enter filename of output or blank for default: "
output_filename = gets.chomp
set_output(action, output_filename) unless output_filename.length.zero?
do_file(filename: source, action: action)
end
run_interface