Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

before refactor cleanup

  • Loading branch information...
commit 5435123b6d6573f0b2d9721eb0f81c567e9c7cbc 1 parent 9066b94
Robert Sosinski authored
View
24 lib/ruby-des.rb
@@ -32,32 +32,20 @@ def initialize(block, key)
@key = key
end
- def encrypt
+ def run(mode)
l = [] # l[0] is the IP_1_L permutation of the block, l[1..16] are the results of each round of encryption.
r = [] # r[0] is the IP_1_R permutation of the block, r[1..16] are the results of each round of encryption.
l << IP_L.collect{|p| block[p - 1]}
r << IP_R.collect{|p| block[p - 1]}
- k = KeySchedule.new(key).sub_keys
-
- 16.times do |i|
- l << r[i]
- r << XOR.run(Feistel.run(r[i], k[i]), l[i])
+ case mode
+ when :encrypt
+ k = KeySchedule.new(key).sub_keys
+ when :decrypt
+ k = KeySchedule.new(key).sub_keys.reverse
end
- return FP.collect{|p| (r.last + l.last)[p - 1]}
- end
-
- def decrypt
- l = [] # l[0] is the IP_1_L permutation of the block, l[1..16] are the results of each round of encryption.
- r = [] # r[0] is the IP_1_R permutation of the block, r[1..16] are the results of each round of encryption.
-
- l << IP_L.collect{|p| block[p - 1]}
- r << IP_R.collect{|p| block[p - 1]}
-
- k = KeySchedule.new(key).sub_keys.reverse
-
16.times do |i|
l << r[i]
r << XOR.run(Feistel.run(r[i], k[i]), l[i])
View
16 lib/ruby-des/feistel.rb
@@ -62,25 +62,22 @@ def self.run(r, k)
m = [] # m[0..7] is the row of the value when performing a s-box lookup.
n = [] # n[0..7] is the column of the value when performing a s-box lookup.
- # Expand the half block using E.
- e = E.collect{|p| r[p - 1]}
+ e = E.collect{|p| r[p - 1]} # Expand r (right half block) using E.
- # X-or the expanded half block with k (the sub key).
- e_xor_k = XOR.run(e, k)
+ e_xor_k = XOR.run(e, k) # X-or e (expanded r) with k (the sub key).
- # Break e_xor_k into 8 6-bit arrays and find both m and m for the s-box lookup.
+ # Break e_xor_k into 8 6-bit arrays and find both m (s-box row) and m (s-box column) for the s-box lookup.
8.times do |j|
b << []
6.times do
b[j] << e_xor_k.shift
end
- # If given the bit array [1, 0, 1, 0, 1, 0]
- m << (b[j].first.to_s + b[j].last.to_s).to_i(2) * 16 # => [1, 0]
- n << b[j][1..4].to_s.to_i(2) # => [0, 1, 0, 1]
+ m << (b[j].first.to_s + b[j].last.to_s).to_i(2) * 16 # [1, 0, 1, 0, 1, 0] => [1, 0]
+ n << b[j][1..4].to_s.to_i(2) # [1, 0, 1, 0, 1, 0] => [0, 1, 0, 1]
end
- # Substitute every 6-bit array with a 4-bit array by using the correct s-box.
+ # Substitute every 6-bit array with the 4-bit array specified by the s-boxes.
b[0] = S1[m[0] + n[0]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[1] = S2[m[1] + n[1]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[2] = S3[m[2] + n[2]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
@@ -90,7 +87,6 @@ def self.run(r, k)
b[6] = S7[m[6] + n[6]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[7] = S8[m[7] + n[7]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
- # Permute the flattened array with P
return P.collect{|p| b.flatten[p - 1]}
end
end
View
6 lib/ruby-des/key_schedule.rb
@@ -31,24 +31,18 @@ def initialize(key)
d = [] # d[0] is the PC_1_R permutation of the key, d[1..16] are the results of each left shift.
k = [] # k[0..15] are the sub keys created by combining c[i] and d[i] and permuting with PC_2.
- # Get c[0] and d[0] by permuting the key with PC_1_L and PC_1_R.
c << PC_1_L.collect{|p| key[p - 1]}
d << PC_1_R.collect{|p| key[p - 1]}
- # Generate 16 sub keys with left-wise rotations and PC_2.
16.times do |i|
-
- # Create two new arrays of bits from the previous arrays of bits.
c << c[i]
d << d[i]
- # Rotate the new arrays of bits left one or two times.
ROTATIONS[i].times do
c[i + 1] << c[i + 1].shift
d[i + 1] << d[i + 1].shift
end
- # Combine the new c and d arrays and permute the result with PC_2.
k << PC_2.collect{|p| (c[i + 1] + d[i + 1])[p - 1]}
end
View
4 test/ctx_test.rb
@@ -4,10 +4,10 @@ class CtxTest < Test::Unit::TestCase
fixtures :ctx
def test_encypt
- assert_equal ctx(:cipher_text), RubyDES::Ctx.new(ctx(:plain_text), ctx(:key)).encrypt
+ assert_equal ctx(:cipher_text), RubyDES::Ctx.new(ctx(:plain_text), ctx(:key)).run(:encrypt)
end
def test_decrypt
- assert_equal ctx(:plain_text), RubyDES::Ctx.new(ctx(:cipher_text), ctx(:key)).decrypt
+ assert_equal ctx(:plain_text), RubyDES::Ctx.new(ctx(:cipher_text), ctx(:key)).run(:decrypt)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.