-
-
Save rhenium/b81355fe816dcfae459cc5eadfc4f6f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This helped me out a ton, thank you!
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
Note: This is the behavior on Ruby <= 2.3. Ruby 2.4 (Ruby/OpenSSL >= 2.0) will raise ArgumentError on line 10.