Browse files

key scheduling is working

  • Loading branch information...
1 parent 05cc7d0 commit 1f74e5e3fde0f06d7b82e1d0fb9eafcc20443edd Robert Sosinski committed Jul 29, 2008
Showing with 33 additions and 12 deletions.
  1. +9 −9 lib/ruby-des.rb
  2. +24 −3 lib/ruby-des/key_schedule.rb
View
18 lib/ruby-des.rb
@@ -22,15 +22,15 @@ module RubyDES
0x22, 0x02, 0x2a, 0x0a, 0x32, 0x12, 0x3a, 0x1a,
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]
- # The binary representation of "ruby-des" with proper parity."
- TEST_KEY = ["1", "1", "1", "0", "0", "1", "0", "1",
- "1", "1", "1", "0", "1", "0", "1", "0",
- "1", "1", "0", "0", "0", "1", "0", "0",
- "1", "1", "1", "1", "0", "0", "1", "0",
- "0", "1", "0", "1", "1", "0", "1", "1",
- "1", "1", "0", "0", "1", "0", "0", "0",
- "1", "1", "0", "0", "1", "0", "1", "1",
- "1", "1", "1", "0", "0", "1", "1", "0"]
+ # The 7-bit binary representation of "ruby-des" with proper parity."
+ TEST_KEY = [1, 1, 1, 0, 0, 1, 0, 1,
+ 1, 1, 1, 0, 1, 0, 1, 0,
+ 1, 1, 0, 0, 0, 1, 0, 0,
+ 1, 1, 1, 1, 0, 0, 1, 0,
+ 0, 1, 0, 1, 1, 0, 1, 1,
+ 1, 1, 0, 0, 1, 0, 0, 0,
+ 1, 1, 0, 0, 1, 0, 1, 1,
+ 1, 1, 1, 0, 0, 1, 1, 0]
class Ctx
def self.new(block, key)
View
27 lib/ruby-des/key_schedule.rb
@@ -9,8 +9,6 @@ class KeySchedule
0x0e, 0x06, 0x3d, 0x35, 0x2d, 0x25, 0x1d,
0x15, 0x0d, 0x05, 0x1c, 0x14, 0x0c, 0x04]
- PC_1 = PC_1_L + PC_1_R
-
PC_2 = [0x0e, 0x11, 0x0b, 0x18, 0x01, 0x05,
0x03, 0x1c, 0x0f, 0x06, 0x15, 0x0a,
0x17, 0x13, 0x0c, 0x04, 0x1a, 0x08,
@@ -24,8 +22,31 @@ class KeySchedule
1, 2, 2, 2, 2, 2, 2, 1]
def self.create(key)
+ c = [] # c[0] is the PC_1_L permutation of the key, c[1..16] are the results of each left shift.
+ 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] with d[i] and permuting with PC_2.
+ # Get c[0] and d[0] by permuting the key with PC_1.
+ c << PC_1_L.collect{|p| key[p - 1]}
+ d << PC_1_R.collect{|p| key[p - 1]}
- return sub_keys
+ # Generate 16 sub keys with left-wise rotations and PC_2.
+ 16.times do |i|
+
+ # Create two new arrays of bits from the current arrays of bits specified by i.
+ 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 arrays (c and d) and permute the result with PC_2.
+ k << PC_2.collect{|p| (c[i + 1] + d[i + 1])[p - 1]}
+ end
+
+ return k
end
end

0 comments on commit 1f74e5e

Please sign in to comment.