Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored, cleaned up and wrote README

  • Loading branch information...
commit 40297e94ccd54992a9103b42957f12ce031fa235 1 parent 2475401
Robert Sosinski authored
Showing with 72 additions and 9 deletions.
  1. +48 −0 README
  2. +23 −8 lib/ruby-des.rb
  3. +1 −1  lib/ruby-des/feistel.rb
View
48 README
@@ -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
View
31 lib/ruby-des.rb
@@ -25,19 +25,23 @@ module RubyDES
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]
class Ctx
- attr_reader :block, :key
+ attr_reader :data, :key
- def initialize(block, key)
- @block = block
- @key = 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"
+ end
+
+ @data = data
+ @key = key
end
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.
- r = [] # r[0] is the IP_1_R permutation of the block, r[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 data 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]}
+ l << IP_L.collect{|p| data[p - 1]}
+ r << IP_R.collect{|p| data[p - 1]}
case mode
when :encrypt
@@ -54,4 +58,15 @@ def run(mode)
return 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}
+ end
+ end
end
View
2  lib/ruby-des/feistel.rb
@@ -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.
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).
Please sign in to comment.
Something went wrong with that request. Please try again.