Skip to content

Instantly share code, notes, and snippets.

@rhenium

rhenium/xyz.rb Secret

Created July 9, 2016 08:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rhenium/b81355fe816dcfae459cc5eadfc4f6f9 to your computer and use it in GitHub Desktop.
Save rhenium/b81355fe816dcfae459cc5eadfc4f6f9 to your computer and use it in GitHub Desktop.
require "openssl"
p plain = ("a".."z").to_a.shuffle.join
cipher = OpenSSL::Cipher.new("aes-128-ecb").encrypt # key_len == 16
cipher.key = "0123456789abcdef" # 16 bytes
p out1 = cipher.update(plain) << cipher.final
cipher = OpenSSL::Cipher.new("aes-128-ecb").encrypt
cipher.key = "0123456789abcdefghi" # 19 bytes
p out2 = cipher.update(plain) << cipher.final
p out1 == out2 # => true
__END__
"kxzghrawqtbylmicovusjdnpef"
"SX\x18z\xB3\x8C\xC3e|\x9B\xD0\xF8=\xC9\xD2Z\x13\x01\xA2w\xD5\xD5\xAE\xCA5q/\xDF\x15Y\xDB\xF5"
"SX\x18z\xB3\x8C\xC3e|\x9B\xD0\xF8=\xC9\xD2Z\x13\x01\xA2w\xD5\xD5\xAE\xCA5q/\xDF\x15Y\xDB\xF5"
true
@rhenium
Copy link
Author

rhenium commented Mar 13, 2017

Note: This is the behavior on Ruby <= 2.3. Ruby 2.4 (Ruby/OpenSSL >= 2.0) will raise ArgumentError on line 10.

@qrush
Copy link

qrush commented Sep 27, 2017

This helped me out a ton, thank you!

@mikedll
Copy link

mikedll commented May 21, 2019

Is the same truncation behavior occurring on the IV assignment operator?

I'm getting an Argument Error when assigning an IV of length > 16, in Ruby 2.4.5. Ruby 2.1.x used to not complain. If I truncate the IV to length 16, the decryption I'm trying to invoke works as I would expect in Ruby 2.4.5.

I think my project's IV is longer than 16 bytes because it was base64 encoded (if I decode I get bytes == 16 in size). But it looks like the bytes beyond the 16th have been simply being dropped from truncation all this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment