views:

54

answers:

2

I'm trying to encrypt some data using the Ezcrypto gem in Ruby. Things work perfectly when running under MRI, but I'm getting strange results when running the same code using JRuby.

Executing the following should produce the same output with repeated called.

Output from MRI:

irb(main):007:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "6KNkObMUfXzyPey+TOGFHbozsmj5bGYqKql2Qava7++j5Moz0Zi2MwRp/FEC\nkpqw\n"
irb(main):008:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "6KNkObMUfXzyPey+TOGFHbozsmj5bGYqKql2Qava7++j5Moz0Zi2MwRp/FEC\nkpqw\n"
irb(main):009:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "6KNkObMUfXzyPey+TOGFHbozsmj5bGYqKql2Qava7++j5Moz0Zi2MwRp/FEC\nkpqw\n"

This output can be decrypted quite happily.

From JRuby though, I get different output on every call:

irb(main):020:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "oX5JKZVkDbpbnrizI2bxkLVfQVfxayBAa0RbG+sfa9OUP1epzAyR7eDf92Bf\nVAiK\n"
irb(main):021:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "mmyKTtn78Dv3eiH2AET1olTkHNPMhtbiXth68Lqoph1nKkYyQH6cSWws/KI5\nwza0\n"
irb(main):022:0> Base64.encode64(EzCrypto::Key.encrypt_with_password "password", "salt","Top secret should not be revealed")
=> "pzDxS0p0CyDCkEY+QVyQKdgP1Of2ZeiNE06InS70ndkHnPwYOFmlH3h+xKXd\nmx54\n"

None of which can be successfully decrypted.

Are there any extra configuration steps in JRuby that need to be done to get this to work?

+1  A: 

Ended up just coding the same thing using OpenSSL directly. Messier, but it works.

I've got a theory that EzCrypto under JRuby regenerates the initialization vector randomly each time encrypt gets called, with no way to set it, hence different output each time.

Haven't had a chance to investigate further

madlep
+1  A: 

Looks like a bug you should file at http://jira.codehaus.org/browse/JRUBY.

Charles Oliver Nutter