Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

AES-256-CBC Encryption between Ruby and PHP

Dear Experts:

I'm starting to find myself guessing at this issue, so it's time to call in the experts!

I want to start by saying that prior to posting this question, I have researched several potential solutions, including a couple from this site. However, none have them have solved the issue and/or I am not applying the potential solution properly.

The ultimate solution will allow passing encrypted data back and forth between a Ruby based system and a PHP  based system. Encrypting and decrypting on both sides using AES-256-CBC. Here are the details:

I have received a key, initialization vector (iv), and an encrypted test phrase - "Eureka! We have a bingo!" This was initiated from the Ruby side and passed to me (the PHP side).

Here are the values:

base64 url safe encoded key:

decoded base64 url safe key:

base64 url safe encoded Initialization Vector (iv):

decoded base64 url safe initialization vector (iv):

encrypted value (Eureka! We have a bingo!):

Regarding the "base64 url safe". The Ruby developer pointed me to (credit to Justin Kelly): 
to pass the encoded key/iv through. I'm not sure if that is doing what it needs to do or not.

The Ruby developer was gracious enough to give me the code sequence he is using to encrypt the value before passing to me:

def self.encrypt(plaintext, key = nil, iv = nil)
# if there is anything to encrypt
    if plaintext.present?
      # assign the var for key and iv from the Base64 encoded copies stored on the server.
      key ||= AppConfig.api['key’]
      iv ||= AppConfig.api['iv’]
      # create a new instance of a Cipher object using the AES-256-CBC algorithm.
      aes ='AES-256-CBC’)
      # assign the decoded Base64 key and iv to the cipher object.
      aes.key = Base64.urlsafe_decode64(key)
      aes.iv = Base64.urlsafe_decode64(iv)
      # add my encrypted texts to the cipher object.  final is now an encrypted string.
      final = aes.update(plaintext) +
      # Base 64 encode final so that it is cleaner when sending back and forth.
      return nil

Open in new window

I was going to list out some of the different code attempts I have made and realized it was pointless, since none of them worked and is why I am here. I can tell you I have tried different combinations of (PHP) pack(), hex2bin and the like, with results varying from absolute garbage to errors - "Size of key is too large for this algorithm", for example.

I will include what I have as the "core" of my attempts:


$org_64_key = 'f2da64041ad7dbabaf8030ff6cbc65c90f9f376080228e3f5498fdb8d5af0b37';
$org_64_iv  = 'c124a4c542ed25380ce4f46ee43c265d';

//encrypted value (Eureka! We have a bingo!):
$cipher_value = 'aPpg0SsMC_xcrw4zZpZ0modgxqcKo35mCAslSMxL1zA=';

$key    = base64_url_decode($org_64_key);
$iv     = base64_url_decode($org_64_iv);
$target = base64_url_decode($cipher_value);

$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $target, MCRYPT_MODE_CBC, $iv );

//credit to Justin Kelly for following two functions
function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_,');

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_,', '+/='));

Open in new window

I tried to keep this on the line between "get to the point" and "need more info", so please let me know if I can offer any more insight. Your help is greatly appreciated.

Thanks in advance,
  • 3
  • 2
1 Solution
Dave BaldwinFixer of ProblemsCommented:
Instead of trying to decode first, you should try to encode/encrypt using all of the same data on both systems.  then compare the 'results' from both systems.  Between PHP and ASP.NET, there is apparently a difference in the way that block padding is or isn't done.  I don't have a page reference for you on that.  Suffice it to say that there are differences in the implementations of this 'standard' method.
mwheeler_fsdAuthor Commented:
Hello Dave,

Thank you for your response. That is an excellent suggestion. In the middle of these situations its easy to overlook what, in most cases, should be obvious. Call it "KISS" or Occam's Razor, breaking this down to the basic steps make's total sense. I will make a request to the Ruby Programmer to send me the cipher unencumbered.

On a side note, I had to chuckle a bit at your last sentence. In my quest to solve this issue, I came across this on Stack Overflow -
"... and , you know, the good thing about standards is, that there are so many to chose from. " with credit to Eugen Rieck at this link:

Story of our lives...

Dave - I'm going to leave this open over night and, unless somebody provides a lock, stock, and barrel solution/explanation, will award you the points.

Thank you!
Dave BaldwinFixer of ProblemsCommented:
Go back to at least 1981 and some famous names for that quote.
mwheeler_fsdAuthor Commented:
Thank you Dave. Your suggestion to break it down is probably the best advice on this type of an issue. This situation could have one thing masking another, and another, etc. I am going to follow your suggestion and work the problem one step at a time and see where it breaks.
Thank you!

**Note - if I discover anything helpful regarding the issue, I will post back.
Dave BaldwinFixer of ProblemsCommented:
Thanks for the points.!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now