-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrysym_ttp.rb
executable file
·49 lines (40 loc) · 951 Bytes
/
crysym_ttp.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
#!/usr/bin/env ruby
# frozen_string_literal: true
##
# Stream cipher
class Cipher
def initialize
@gamma = String.new
end
def calc_gamma(text, cipher)
raise 'Size of text and cipher is not equal' if text.bytesize != cipher.bytesize
text.each_byte.with_index do |b, i|
@gamma << (b ^ cipher[i].ord)
end
end
def encode(text)
if @gamma.bytesize < text.bytesize
raise "Size of gamma (#{@gamma.bytesize}) is not enough to encode text (#{text.bytesize})"
end
out = String.new
text.each_byte.with_index do |b, i|
out << (b ^ @gamma[i].ord)
end
out
end
end
print 'Text1: '
text1 = $stdin.readline.strip
print 'Cipher1: '
cipher1 = $stdin.readline.strip
print 'Cipher2: '
cipher2 = $stdin.readline.strip
cipher = Cipher.new
begin
cipher.calc_gamma(text1, [cipher1].pack('H*'))
text2 = cipher.encode([cipher2].pack('H*'))
rescue StandardError => e
puts e
exit
end
puts text2