0

If I write the following code

mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $raw, MCRYPT_MODE_CBC, $iv);

How to write in ruby?

4 Answers 4

1

Here is Ruby equivalent using OpenSSL,

  cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  cipher.encrypt
  cipher.key = key
  cipher.iv = iv
  encrypted = cipher.update(raw)
  encrypted << cipher.final
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answering but bad decrypt (OpenSSL::Cipher::CipherError) occured What's wrong?
If your keysize is 32 bytes you need to write 'OpenSSL::Cipher::Cipher.new("aes-256-cbc")'
1

You can use the openssl library for this. I think what you need is here for symmetric key encryption:

http://stuff-things.net/2008/02/05/encrypting-lots-of-sensitive-data-with-ruby-on-rails/

Comments

0

Can ezcrypto help? Default it uses standard aes-128-cbc but does know other algorithms.

Comments

0

MCRYPT_RIJNDAEL_128 denotes the blocksize 16 Bytes and 3 key sizes are possible

  1. $key ~ 16 Bytes (AES-128)

    cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")

  2. $key ~ 24 Bytes (AES-192)

    cipher = OpenSSL::Cipher::Cipher.new("aes-192-cbc")

  3. $key ~ 32 Bytes (AES-256)

    cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")

And then we can use the following code to perform encryption

cipher.encrypt
cipher.key = key
cipher.iv = iv
encrypted = cipher.update(raw)
encrypted << cipher.final

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.