gcristelli
3/24/2009 4:33:00 AM
On 24 Mar, 04:21, Felipe Coury <felipe.co...@gmail.com> wrote:
> Hello there!
>
> I need to mimic what MySQL does when encrypting and decrypting strings
> using built-in functions AES_ENCRYPT() and AES_DECRYPT().
> ...
> However if I attempt to use OpenSSL library, there's no way I can make
> it work:
>
> cipher = OpenSSL::Cipher::Cipher.new("AES-128-ECB")
> cipher.padding = 0
> cipher.key = key
> cipher.decrypt
>
> user = User.find(1)
> cipher.update(user.password) << cipher.final #=>
> "########gf####\027\227"
>
I use the following code for encrypt/decrypt:
@cipherAES256=OpenSSL::Cipher::AES256.new("CBC") if @cipherAES256.nil?
@cipherAES256.encrypt
@cipherAES256.key=key
ct = @cipherAES256.update(plainPassword) + @cipherAES256.final
password=ct.unpack("H*")[0]
@cipherAES256=OpenSSL::Cipher::AES256.new("CBC") if @cipherAES256.nil?
@cipherAES256.decrypt
@cipherAES256.key=key
ct = @cipherAES256.update([password].pack("H*")) + @cipherAES256.final
I don't know if it can helps you (it uses 256 and CBC), try changing
your code from
cipher.update(user.password) << cipher.final
to
cipher.update([user.password].pack("H*")) << cipher.final
Giovanni