Skip to content

Commit

Permalink
refactored, cleaned up and wrote README
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Sosinski committed Jul 31, 2008
1 parent 2475401 commit 40297e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
48 changes: 48 additions & 0 deletions README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,48 @@
== Ruby-DES

Ruby-DES is a full Ruby implementation of the Data Encryption Standard. The purpose of this
project was to allow Ruby programmers interested in cryptography a glimpse of how a robust
cryptographic algorithm functions in a language they understand.

NOTE: DES is deprecated, and as such, you should not use this implementation in any project you
plan on developing. I highly recommend the AES, TwoFish or Serpent algorithms through the OpenSSL
library instead.

== Running Ruby-DES

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.

des = RubyDES::Ctx.new(data.bit_array, key.bit_array)

Finally, let it rip.

encrypted_data_bit_array = des.run(:encrypt)

You will then be returned a DES encrypted bit array 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
fashion as before.

un_des = RubyDES::Ctx.new(encrypted_data_bit_array, key.bit_array)

And run the DES with the key schedule reversed.

un_encrypted_data_bit_array = un_des.run(:decrypt)

You can then check to see if it all worked.

data.bit_array.eql?(un_encrypted_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
31 changes: 23 additions & 8 deletions lib/ruby-des.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ module RubyDES
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19] 0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]


class Ctx class Ctx
attr_reader :block, :key attr_reader :data, :key


def initialize(block, key) def initialize(data, key)
@block = block unless data.is_a?(Array) and key.is_a?(Array) and data.size.eql?(64) and key.size.eql?(64)
@key = key raise "RubyDES::InvalidBlockFormat: data and key attributes must be passed bit arrays that are 64 bits in size"
end

@data = data
@key = key
end end


def run(mode) 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. 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 block, r[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| block[p - 1]} l << IP_L.collect{|p| data[p - 1]}
r << IP_R.collect{|p| block[p - 1]} r << IP_R.collect{|p| data[p - 1]}


case mode case mode
when :encrypt when :encrypt
Expand All @@ -54,4 +58,15 @@ def run(mode)
return FP.collect{|p| (r.last + l.last)[p - 1]} return FP.collect{|p| (r.last + l.last)[p - 1]}
end end
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}
end
end
end end
2 changes: 1 addition & 1 deletion lib/ruby-des/feistel.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.run(r, k)
m = [] # m[0..7] is the row of the value when performing a s-box lookup. 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. n = [] # n[0..7] is the column of the value when performing a s-box lookup.


e = E.collect{|p| r[p - 1]} # Expand r (right half block) using E. e = E.collect{|p| r[p - 1]} # Expand r (right half data block) using E.


e_xor_k = XOR.run(e, k) # X-or e (expanded r) with k (the sub key). e_xor_k = XOR.run(e, k) # X-or e (expanded r) with k (the sub key).


Expand Down

0 comments on commit 40297e9

Please sign in to comment.