73556

PHP Decrypting AES returns padding at front of string?

I've been wrestling with decrypting a given string, generated by a remote ColdFusion server, in PHP using AES in CBC mode with PKCS5 padding. I've gotten to the point where I can decrypt the string almost perfectly, the issue is there appears to be some cruft at the beginning of the string. I thought padding happened at the end, but looking at the decrypted string, there's nothing at the end, but the beginning is padded out so the string is 64 characters long (the original string is 32 characters long.) I attempted to switch my padding removal code to look at the beginning instead of the end, but those characters don't provide any information I can use to decipher how much padding to remove, so I think they are coming from somewhere else. Here's my code so far

function decrypt($hash) { $enc_key = "Oq2vh+gswPn2CRPccODtKg=="; $cipher = "rijndael-128"; $str = mcrypt_decrypt($cipher, base64_decode($enc_key), base64_decode($hash), MCRYPT_MODE_CBC); $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $pad = ord($str[($len = strlen($str)) - 1]); $len = strlen($str); $decrypted = substr($str, 0, strlen($str) - $pad); if($decrypted != NULL) { $params = explode ('|', $decrypted); } return (object)array( 'input' => $hash, 'pad' => $pad, 'len' => $len, 'blocksize' => $block, 'aes_key' => $enc_key, 'cipher' => $cipher, 'result' => $params, 'decrypted' => $decrypted, 'padded' => $str );

If I encrypt the string (using ColdFusion, '"AES/CBC/PKCS5Padding"' algorithm and base64 encoding):

"test@example.com|test|1400863515"

I get the encrypted string:

QRO04vmtw76Qvl0hscmYZ/SFGNv/8d88H9kT60JA5IJdg/KMT7udrn2IZuQzkOPvLjXoc4novzTMGsk0CMxjvg==

and when I run this through the above PHP function, I get this as output:

¹¾Sò'->äe¿fÏäJ±test@example.com|test|1400863515

What are those characters at the beginning? Why is there no padding at the end? I've read through dozens of posts on SO and elsewhere (which is how I got this far) but this last piece has me scratching my head.

Answer1:

(From comments ...)

CBC mode requires an iv. "Decrypting with the incorrect IV causes the first block of plaintext to be corrupt ...". Try using the same iv on both sides

Recommend

  • Need help diagnosing a 500 error in Heroku
  • Which is more readable (C++ = )
  • Automating Saving a file from the web with the IE COM object in Powershell
  • encrypt using node.js crypto aes256 and decrypt using python2.7 PyCrypto
  • Remove annotation while keeping plot matplotlib
  • Are there Core Data call back methods?
  • Ruby regex to remove all consecutive letters from string
  • Rails 5 - Google Maps - Javascript error - initMap is not a function - fixing one js issue creates a
  • custom string delimiters stringtemplate-4
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Validate jQuery plugin, field not required
  • Who propagate bugfixes across branches (corporate development)?
  • Hide HTML elements without javascript, only CSS
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • xcode don't localize specific strings
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Copy to all folders batch file?
  • Zurb Foundation _global.scss meta styles for js?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • Moving mysql files across servers
  • How to access EntityManager inside Entity class in EJB3
  • ilmerge with a PFX file
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Join two tables and save into third-sql
  • vba code to select only visible cells in specific column except heading
  • When should I choose bucket sort over other sorting algorithms?
  • How to make Safari send if-modified-since header?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • How to disable jQuery.jplayer autoplay?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Running Map reduces the dimensions of the matrices
  • java string with new operator and a literal