forked from robertsosinski/ruby-des
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-des.rb
executable file
·92 lines (74 loc) · 3.02 KB
/
ruby-des.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
#encoding: utf-8
require './ruby-des/feistel'
require './ruby-des/key_schedule'
require './ruby-des/xor'
module RubyDES
# the following arrays are the initial and final permutation matrixes
IP_L = [0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12, 0x0a, 0x02,
0x3c, 0x34, 0x2c, 0x24, 0x1c, 0x14, 0x0c, 0x04,
0x3e, 0x36, 0x2e, 0x26, 0x1e, 0x16, 0x0e, 0x06,
0x40, 0x38, 0x30, 0x28, 0x20, 0x18, 0x10, 0x08]
IP_R = [0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09, 0x01,
0x3b, 0x33, 0x2b, 0x23, 0x1b, 0x13, 0x0b, 0x03,
0x3d, 0x35, 0x2d, 0x25, 0x1d, 0x15, 0x0d, 0x05,
0x3f, 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f, 0x07]
FP = [0x28, 0x08, 0x30, 0x10, 0x38, 0x18, 0x40, 0x20,
0x27, 0x07, 0x2f, 0x0f, 0x37, 0x17, 0x3f, 0x1f,
0x26, 0x06, 0x2e, 0x0e, 0x36, 0x16, 0x3e, 0x1e,
0x25, 0x05, 0x2d, 0x0d, 0x35, 0x15, 0x3d, 0x1d,
0x24, 0x04, 0x2c, 0x0c, 0x34, 0x14, 0x3c, 0x1c,
0x23, 0x03, 0x2b, 0x0b, 0x33, 0x13, 0x3b, 0x1b,
0x22, 0x02, 0x2a, 0x0a, 0x32, 0x12, 0x3a, 0x1a,
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]
class Ctx
attr_reader :data, :key
def initialize(data, key)
unless data.is_a?(RubyDES::Block) and key.is_a?(RubyDES::Block)
raise "RubyDES::InvalidBlockFormat: Data and key must be a Block object."
end
@data = data
@key = key
end
def encrypt
self.run(:encrypt)
end
def decrypt
self.run(:decrypt)
end
protected
def run(operation)
# do the initial permutation
# left[i] and right[i] are the parts of the data block at each step of algorithm
left = [] # l[0] is the IP_1_L permutation of the data block, l[1..16] are the results of each round of encryption.
right = [] # r[0] is the IP_1_R permutation of the data block, r[1..16] are the results of each round of encryption.
left << IP_L.collect{|p| data.bit_array[p - 1]}
right << IP_R.collect{|p| data.bit_array[p - 1]}
# get keys
keys = KeySchedule.new(key.bit_array).sub_keys
keys.reverse! if operation == :decrypt
# rounds of encryption
16.times do |i|
left << right[i]
right << XOR.run(Feistel.run(right[i], keys[i]), left[i])
end
# final permutation
return RubyDES::Block.new(FP.collect{|p| (right.last + left.last)[p - 1]})
end
end
class Block
attr_reader :string, :bit_array
def initialize(input)
if input.is_a?(String)
raise "RubyDES::InvalidStringLength: Input String must contain (8) characters." unless input.length.eql?(8)
@string = input
@bit_array = input.unpack('B*')[0].split('').map(&:to_i)
elsif input.is_a?(Array)
raise "RubyDES::InvalidArraySize: Input Array must contain (64) bits." unless input.size.eql?(64)
@string = [input.join].pack('B*')
@bit_array = input
else
raise "RubyDES::InvalidFormat: Input must be a String or an Array."
end
end
end
end