Permalink
Browse files

building out the fiestel module

  • Loading branch information...
1 parent 8b25952 commit 5382aafcc67ac3d2ce2b5facb5c4ca7cb59ff4b2 Robert Sosinski committed Jul 30, 2008
Showing with 48 additions and 24 deletions.
  1. +39 −10 lib/ruby-des.rb
  2. +3 −7 lib/ruby-des/feistel.rb
  3. +1 −7 lib/ruby-des/key_schedule.rb
  4. +5 −0 lib/ruby-des/x-or.rb
View
49 lib/ruby-des.rb
@@ -4,14 +4,15 @@
require 'ruby-des/key_schedule'
module RubyDES
- IP = [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,
- 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]
+ 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,
@@ -22,6 +23,16 @@ module RubyDES
0x22, 0x02, 0x2a, 0x0a, 0x32, 0x12, 0x3a, 0x1a,
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]
+ # The 8-bit binary representation of "security"
+ TEST_MESSAGE = [0, 1, 1, 1, 0, 0, 1, 1,
+ 0, 1, 1, 0, 0, 1, 0, 1,
+ 0, 1, 1, 0, 0, 0, 1, 1,
+ 0, 1, 1, 1, 0, 1, 0, 1,
+ 0, 1, 1, 1, 0, 0, 1, 0,
+ 0, 1, 1, 0, 1, 0, 0, 1,
+ 0, 1, 1 ,1, 0, 1, 0, 0,
+ 0, 1, 1, 1, 1, 0, 0, 1]
+
# 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,
@@ -33,12 +44,30 @@ module RubyDES
1, 1, 1, 0, 0, 1, 1, 0]
class Ctx
- def self.new(block, key)
-
+ attr_reader :block, :key
+
+ def initialize(block, key)
+ @block = block
+ @key = key
end
def encrypt
+ 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(Fiestel.run(r[i], k[i], i), l[i])
+ end
+
+ k << PC_2.collect{|p| (c[i + 1] + d[i + 1])[p - 1]}
+ output = FP.collect{|p| (l.last + r.last)[p - 1]}
end
def decrypt
View
10 lib/ruby-des/feistel.rb
@@ -1,4 +1,4 @@
-class Feistel
+module Feistel
E = [0x20, 0x01, 0x02, 0x03, 0x04, 0x05,
0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
@@ -56,12 +56,8 @@ class 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 initialize(half_block, sub_key, round)
- return [half_block, sub_key, round]
- end
-
- def run
+ def self.run(half_block, sub_key, round)
+ return output
end
end
View
8 lib/ruby-des/key_schedule.rb
@@ -29,30 +29,24 @@ def initialize(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.
+ 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 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
- # 16 steps later, you have your 16 48 bit sub keys.
@sub_keys = k
end
end
View
5 lib/ruby-des/x-or.rb
@@ -0,0 +1,5 @@
+module XOR
+ def self.run
+
+ end
+end

0 comments on commit 5382aaf

Please sign in to comment.