Hi,
When using this library to talk to an OpenSSL server, I needed this little fix to get the padding right.
/Daniel
--- /usr/lib/ruby/gems/1.8/gems/crypt-1.1.4/crypt/cbc.rb.orig 2007-02-06 10:40:54.000000000 +0100
+++ /usr/lib/ruby/gems/1.8/gems/crypt-1.1.4/crypt/cbc.rb 2007-02-06 10:41:37.000000000 +0100
@@ -43,10 +43,8 @@
# bytes in the final block
block = '' if block.nil?
buffer = block.split('')
- remainingMessageBytes = buffer.length
- # we use 7-bit characters to avoid possible strange behavior on the Mac
- remainingMessageBytes.upto(block_size()-2) { buffer << rand(128).chr }
- buffer << remainingMessageBytes.chr
+ remainingMessageBytes = block_size() - buffer.length
+ buffer << remainingMessageBytes.chr * remainingMessageBytes
block = buffer.join('')
block = block ^ chain
encrypted = encrypt_block(block)
@@ -67,7 +65,7 @@
# write the final block, omitting the padding
buffer = plainText.split('')
- remainingMessageBytes = buffer.last.unpack('C').first
+ remainingMessageBytes = block_size() - buffer.last.unpack('C').first
remainingMessageBytes.times { plainStream.write(buffer.shift) }
end
|