Permalink
Browse files

RubyDES::Ctx now takes blocks as input and returns blocks as output.

  • Loading branch information...
1 parent 40297e9 commit b3d62b4d4b42a98fc9336a139bc79a91a4eaa0e6 Robert Sosinski committed Aug 1, 2008
Showing with 61 additions and 55 deletions.
  1. +9 −10 README
  2. +23 −14 lib/ruby-des.rb
  3. +2 −2 lib/ruby-des/feistel.rb
  4. +16 −25 test/ctx.yml
  5. +11 −4 test/ctx_test.rb
View
19 README
@@ -15,34 +15,33 @@ Using RubyDES is pretty easy. First, construct a new data and key block.
data = RubyDES::Block.new('hushhush')
key = RubyDES::Block.new('password')
-Then, build a new <tt>RubyDES::Ctx</tt> object and supply the bit arrays for both the data and key
-block.
+Then, build a new <tt>RubyDES::Ctx</tt> object and supply the data and key block.
- des = RubyDES::Ctx.new(data.bit_array, key.bit_array)
+ des = RubyDES::Ctx.new(data, key)
Finally, let it rip.
- encrypted_data_bit_array = des.run(:encrypt)
+ encrypted_data = des.run(:encrypt)
-You will then be returned a DES encrypted bit array that is completely secure against eavesdropping
+You will then be returned a DES encrypted block that is completely secure against eavesdropping
(if it were still 1997).
-To decrypt an encrypted array if bits, just build a new <tt>RubyDES::Ctx</tt> object in similar
+To decrypt an encrypted data block, just build a new <tt>RubyDES::Ctx</tt> object in similar
fashion as before.
- un_des = RubyDES::Ctx.new(encrypted_data_bit_array, key.bit_array)
+ un_des = RubyDES::Ctx.new(encrypted_data, key)
And run the DES with the key schedule reversed.
- un_encrypted_data_bit_array = un_des.run(:decrypt)
+ decrypted_data = un_des.run(:decrypt)
You can then check to see if it all worked.
- data.bit_array.eql?(un_encrypted_data_bit_array)
+ data.bit_array.eql?(decrypted_data.bit_array)
Enjoy!
== Feedback
If you have any questions, comments or just want to talk shop about crypto, feel free to reach me
-through my website at http://www.robertsosinski.com
+through my website at http://www.robertsosinski.com.
View
@@ -28,45 +28,54 @@ class Ctx
attr_reader :data, :key
def initialize(data, key)
- unless data.is_a?(Array) and key.is_a?(Array) and data.size.eql?(64) and key.size.eql?(64)
- raise "RubyDES::InvalidBlockFormat: data and key attributes must be passed bit arrays that are 64 bits in size"
+ unless data.is_a?(RubyDES::Block) and key.is_a?(RubyDES::Block)
+ raise "RubyDES::InvalidBlockFormat: Data and key must be a Block object."
end
@data = data
@key = key
end
- def run(mode)
+ def run(operation)
l = [] # l[0] is the IP_1_L permutation of the data block, l[1..16] are the results of each round of encryption.
r = [] # r[0] is the IP_1_R permutation of the data block, r[1..16] are the results of each round of encryption.
- l << IP_L.collect{|p| data[p - 1]}
- r << IP_R.collect{|p| data[p - 1]}
+ l << IP_L.collect{|p| data.bit_array[p - 1]}
+ r << IP_R.collect{|p| data.bit_array[p - 1]}
- case mode
+ case operation
when :encrypt
- k = KeySchedule.new(key).sub_keys
+ k = KeySchedule.new(key.bit_array).sub_keys
when :decrypt
- k = KeySchedule.new(key).sub_keys.reverse
+ k = KeySchedule.new(key.bit_array).sub_keys.reverse
end
16.times do |i|
l << r[i]
r << XOR.run(Feistel.run(r[i], k[i]), l[i])
end
- return FP.collect{|p| (r.last + l.last)[p - 1]}
+ return RubyDES::Block.new(FP.collect{|p| (r.last + l.last)[p - 1]})
end
end
class Block
attr_reader :string, :bit_array
- def initialize(string)
- raise "RubyDES::InvalidStringSize: input string must contain (8) characters" unless string.length.eql?(8)
-
- @string = string
- @bit_array = string.unpack('B*').join.split('').collect{|b| b.to_i}
+ def initialize(input)
+ if input.is_a?(String)
+ raise "RubyDES::InvalidStringLength: Input Array must contain (8) characters." unless input.length.eql?(8)
+
+ @string = input
+ @bit_array = input.unpack('B*').join.split('').collect{|b| b.to_i}
+ elsif input.is_a?(Array)
+ raise "RubyDES::InvalidArraySize: Input Array must contain (64) bits." unless input.size.eql?(64)
+
+ @string = input.join.to_a.pack('B*')
+ @bit_array = input
+ else
+ raise "RubyDES::InvalidFormat: Input must be a String or an Array."
+ end
end
end
end
View
@@ -75,8 +75,8 @@ def self.run(r, k)
b[j] << e_xor_k.shift
end
- 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]
+ m << (b[j].first.to_s + b[j].last.to_s).to_i(2) * 16 # [1, 0, 1, 0, 1, 0] => [1, 0] => 2 => 32 => 3rd row.
+ n << b[j][1..4].to_s.to_i(2) # [1, 0, 1, 0, 1, 0] => [0, 1, 0, 1] => 5 => 6th column.
end
# Substitute every 6-bit array with the 4-bit array specified by the appropriate s-box.
View
@@ -1,26 +1,17 @@
-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]
+decrypted_bit_array: [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]
-plain_text: [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]
-
-cipher_text: [0, 1, 0, 1, 1, 1, 1, 0,
- 1, 1, 1, 0, 1, 1, 0, 0,
- 1, 0, 1, 0, 1, 1, 1, 0,
- 0, 0, 1, 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 1, 1,
- 1, 1, 1, 1, 1, 1, 0, 1,
- 0, 1, 0, 0, 1, 1, 0, 0,
- 0, 1, 0, 1, 0, 0, 1, 0]
+encrypted_bit_array: [0, 0, 0, 0, 1, 0, 1, 0,
+ 1, 1, 0, 1, 1, 1, 1, 0,
+ 0, 1, 0, 1, 0, 0, 1, 0,
+ 1, 0, 1, 0, 0, 1, 0, 0,
+ 0, 1, 1, 1, 0, 0, 1, 0,
+ 0, 0, 0, 0, 0, 1, 0, 1,
+ 0, 0, 0, 0, 0, 0, 1, 0,
+ 0, 1, 0, 1, 0, 0, 0, 1]
View
@@ -3,11 +3,18 @@
class CtxTest < Test::Unit::TestCase
fixtures :ctx
- def test_encypt
- assert_equal ctx(:cipher_text), RubyDES::Ctx.new(ctx(:plain_text), ctx(:key)).run(:encrypt)
+ def setup
+ @data = RubyDES::Block.new('security')
+ @key = RubyDES::Block.new('ruby-des')
end
- def test_decrypt
- assert_equal ctx(:plain_text), RubyDES::Ctx.new(ctx(:cipher_text), ctx(:key)).run(:decrypt)
+ def test_run
+ encrypted = RubyDES::Ctx.new(@data, @key).run(:encrypt)
+
+ assert_equal ctx(:encrypted_bit_array), encrypted.bit_array
+
+ decrypted = RubyDES::Ctx.new(encrypted, @key).run(:decrypt)
+
+ assert_equal ctx(:decrypted_bit_array), decrypted.bit_array
end
end

0 comments on commit b3d62b4

Please sign in to comment.