3

I am trying to encrypt using Rijndael 256-bit block size, 256-bit key size, ECB mode and zero's for padding.

I was trying to use OpenSSL::Cipher::AES.new(256, :ECB) but I cannot for the life of me get the correct result I am looking for.

I have a solution in C# but I'm having trouble getting the Ruby equivalent for it.

In the C# code RijndaelManaged is used for encryption and what I'm having trouble with in particular is RijndaelManaged is ok taking in a byte array for the key and the object to be encrypted. I can't find a Ruby library that will do the same for me. OpenSSL::Cipher::AES will only take in strings.

Is there anything similar like this in Ruby? Google has failed me in finding anything and I'm not an encryption guru by any means. Any help or just point me in the right direction would be amazing. I just cannot figure out for the life of me. I have a related SO question to this if anyone wants to take a look. The answer is perfect I just cannot translate it to Ruby.

This question isn't a duplicate. I mean it is similar to the question it is listed as a duplicate of however the answer doesn't help me at all. The answer they link to is a library that uses :cbc but I need :ecb mode for the Rijndael encryption. I agree though the questions are very similar. Just the answer doesn't help me hence me asking this question.

Gabriel
  • 621
  • 7
  • 19
  • 1
    RijndaelManaged is not standard. Rijndael was selected as AES and made standard. There is no 256-bit block size in AES as in Rijndael. It is better to change 256-bit block size to the standard AES. – kelalaka Dec 21 '18 at 18:35
  • I see. For some reason the third-party service I am trying to use wants it to be a Rijndael implementation with a block size of 256 bits. I'm not experience with this but are there implementations of Rijndael that can use a 256-bit block size? – Gabriel Dec 21 '18 at 18:39
  • 1
    This site http://crypt.rubyforge.org/rijndael.html (currently down) claimed that it has. may be you can find somewhere its versions? – kelalaka Dec 21 '18 at 18:49
  • Thanks for that. i did try it but it only has a CBC implementation. Haven't had much luck searching for a ruby implementation of it. Thanks for your help but I'm starting to feel like i may have to implement it myself – Gabriel Dec 21 '18 at 18:51
  • 1
    And, I should say that ECB is insecure, prefer AES-GCM since it is an authenticated encryption. If you really need ECB, it is not hard to change a CBC code into ECB code. – kelalaka Dec 21 '18 at 18:53
  • Yeah I've been reading about ECB's insecurity a lot. I think the third party I'm using just hasn't updated their encryption methods yet. I'm not quite sure why. I'll look into changing the CBC code to ECB code! Thanks for your help. You've been super helpful! – Gabriel Dec 21 '18 at 18:54
  • You can just call the CBC code for each block with a zero IV. Simply pad the last block of plaintext yourself with zero bytes. Note that there are two ways of zero padding: one that doesn't pad if the plaintext is already block-aligned and one that does. You may want to test the C# code with a single block of plaintext to see which one it is, I'm not sure out of the top of my head. – Maarten Bodewes Dec 24 '18 at 13:02
  • Possible duplicate of [Decrypting PHP MCRYPT\_RIJNDAEL\_256 in Ruby](https://stackoverflow.com/questions/23745059/decrypting-php-mcrypt-rijndael-256-in-ruby) – Ilmari Karonen Dec 30 '18 at 22:57

1 Answers1

0

I think the only gem in existence that could accomplish this task for me turned out to be the ruby-mcrypt gem. For any future generations who are in need of a library to encrypt some lesser used and potentially unsafe encryption technique.

Gabriel
  • 621
  • 7
  • 19