Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

s-boxes working, just steps away from making a full feistel network!

  • Loading branch information...
commit df8bf3663fbe3557f354bbf5abdfb0418a725a4e 1 parent bf99757
@robertsosinski authored
Showing with 168 additions and 119 deletions.
  1. +25 −8 lib/ruby-des/feistel.rb
  2. +143 −111 test/key_schedule.yml
View
33 lib/ruby-des/feistel.rb
@@ -56,14 +56,20 @@ module Feistel
0x01, 0x0f, 0x0d, 0x08, 0x0a, 0x03, 0x07, 0x04, 0x0c, 0x05, 0x06, 0x0b, 0x00, 0x0e, 0x09, 0x02,
0x07, 0x0b, 0x04, 0x01, 0x09, 0x0c, 0x0e, 0x02, 0x00, 0x06, 0x0a, 0x0d, 0x0f, 0x03, 0x05, 0x08,
0x02, 0x01, 0x0e, 0x07, 0x04, 0x0a, 0x08, 0x0d, 0x0f, 0x0c, 0x09, 0x00, 0x03, 0x05, 0x06, 0x0b]
-
- def self.run(half_block, k, round)
- e = [] # e[0..47] is the expanded half block created with the E permutation.
- e_xor_k = [] # e_xor_k[0..47] is the result of x-oring e with the current sub key.
+
+ S = [S1, S2, S3, S4, S5, S6, S7, S8]
+
+ # Some test data
+ #
+ # hb = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1]
+ # k = [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1]
+
+ def self.run(half_block, k)
b = [] # b[0..7] is e_xor_k prepped as 8 6-bit arrays for sbox substitution.
+ m = [] #
+ n = []
- e << E.collect{|p| half_block[p - 1]}
-
+ e = E.collect{|p| half_block[p - 1]}
e_xor_k = XOR.run(e, k)
8.times do |i|
@@ -71,9 +77,20 @@ def self.run(half_block, k, round)
6.times do
b[i] << e_xor_k.shift
end
+
+ m << (b[i].first.to_s + b[i].last.to_s).to_i(2) * 16
+ n << b[i][1..4].to_s.to_i(2)
end
- row = (e_xor_k.shift + e_xor_k.pop).to_i
- col = e_xor_k.to_s.to_i(2)
+ 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}
+ b[3] = S4[m[3] + n[3]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
+ b[4] = S5[m[4] + n[4]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
+ b[5] = S6[m[5] + n[5]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
+ 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}
+
+ return b
end
end
View
254 test/key_schedule.yml
@@ -7,114 +7,146 @@ test_key: [1, 1, 1, 0, 0, 1, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 1, 0]
-sub_key_1: [1, 0, 1, 0, 1, 1, 0, 1,
- 1, 1, 0, 1, 1, 0, 1, 1,
- 0, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0, 1,
- 1, 0, 1, 0, 1, 1, 1, 1]
-
-sub_key_2: [1, 0, 1, 0, 1, 1, 1, 1,
- 1, 1, 1, 1, 0, 1, 1, 1,
- 1, 1, 0, 1, 1, 0, 1, 0,
- 0, 0, 1, 0, 0, 1, 1, 1,
- 1, 1, 1, 0, 1, 1, 0, 1,
- 0, 0, 1, 1, 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