-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebc.rb
41 lines (36 loc) · 862 Bytes
/
ebc.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
require 'des.rb'
class EBC
attr_writer :des, :data
def initialize(des, data)
@des = des
if data.instance_of? String
@data = data.to_bin
self.add_pad(64)
else
@data = data
end
end
def add_pad(multiple = 64)
@data = @data.to_bits unless @data.instance_of? Array
while(@data.size % multiple != 0)
@data << 0
end
end
def encipher()
blocks = @data.splitBlocks(64)
cipherText = []
blocks.each do |block|
cipherText << @des.encrypt(block)
end
cipherText
end
def decipher()
@data.flatten!
blocks = @data.splitBlocks(64)
plainText = []
blocks.each do |block|
plainText << @des.decrypt(block)
end
plainText
end
end