AES-256-CBC Encryption between Ruby and PHP

Posted on 2015-01-15
Last Modified: 2015-01-16
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,
Question by:mwheeler_fsd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 83

Accepted Solution

Dave Baldwin earned 500 total points
ID: 40552474
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.

Author Comment

ID: 40552746
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!
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40552773
Go back to at least 1981 and some famous names for that quote.

Author Closing Comment

ID: 40554253
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.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40554302
Thanks for the points.!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Encryption for Business Encryption ( ensures the safety of our data when sending emails. In most cases, to read an encrypted email you must enter a secret key that will enable you to decrypt the email. T…
Businesses who process credit card payments have to adhere to PCI Compliance standards. Here’s why that’s important.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question