Php code decrypts encrypted coldfusion string

we are trying to decrypt an encrypted coldfusion string using AES / OFB / Nopadding in PHP. However, we got an even worse impression, we will try the whole solution here, but still cannot make it work.

here is the code from CF

<cfsetting enablecfoutputonly="Yes"> <!--- Set encoding ---> <cfset k_strCharset="UTF-8"> <cfcontent type="text/html; charset=#k_strCharset#"> <cfset setEncoding("URL", "#k_strCharset#")> <cfset setEncoding("FORM", "#k_strCharset#")> <!--- Get variables ---> <cfif IsDefined("FORM.K1")><cfset fv_strK1="#FORM.K1#"><cfelse><cfset fv_strK1=""></cfif><!--- xxx ---> <cfif IsDefined("FORM.S1")><cfset fv_strS1="#FORM.S1#"><cfelse><cfset fv_strS1=""></cfif> <cfif IsDefined("FORM.S2")><cfset fv_strS2="#FORM.S2#"><cfelse><cfset fv_strS2=""></cfif> <!--- Encrypt / Decrypt ---> <cfif fv_strK1 is "xxx"> <cfif fv_strS1 is not ""> <cfset fv_strS2 = Encrypt(fv_strS1, fv_strK1, "AES/OFB/NoPadding", "BASE64")> <cfelseif fv_strS2 is not ""> <cfset fv_strS1 = Decrypt(fv_strS2, fv_strK1, "AES/OFB/NoPadding", "BASE64")> </cfif> <cfset fv_strS3 = ""> <cfset fv_strS4 = ""> <cfset fv_strS5 = ""> </cfif> <cfsetting enablecfoutputonly="No"> 

then we do php like

 <?php $z = "bf19zWnbPmJxOvzRuP85Bw=="; $encrypted_string="q2SYE7hWWltsBw5byuwl/IkGmOOm+94="; $source_text = html_entity_decode(getDecrypt($encrypted_string, $z), ENT_NOQUOTES, 'UTF-8'); //echo trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_OFB)); echo "<br>" . $z . "<br>"; // echo trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, base64_decode($z), base64_decode($encrypted_string), MCRYPT_MODE_OFB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_OFB), MCRYPT_RAND))); echo "\n\nPlain-Text:\n" . $source_text . "\n"; // Functions function getDecrypt($str, $key) { return ofb_decrypt(base64_decode($str),$key); } function ofb_decrypt($str, $key, $iv = ' ' ) { if ($iv==' ' & strlen($str) < 16) return false; $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ' ' , MCRYPT_MODE_NOFB, ' '); //RECEOVER IV $iv_size = mcrypt_enc_get_iv_size($td); if (empty($iv)) { $iv = substr($str,0,$iv_size); $str = substr($str,$iv_size); } // initialize encryption mcrypt_generic_init($td, $key, $iv); // decrypt $decrypted_string = mdecrypt_generic($td, $str); // terminate decrtypion mcrypt_generic_deinit($td); mcrypt_module_close($td); return $decrypted_string; } ?> 

$ encrypted_string is created using the CF script above.

then we got the result: = @ & O% NSC # p:

Really appreciate if anyone can give me a hint.

Thank you

+4
source share
2 answers

I managed to get โ€œtestingโ€ (and some garbage) from the string using the poorly documented argument โ€œncfbโ€ for mcrypt_decrypt ( http://php.net/manual/en/mcrypt.constants.php ). I think the garbage is related to the block size ... some additional input examples would be helpful.

 function decryptColdfusionString($key, $data) { $retVal = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, base64_decode($key), base64_decode($data), 'ncfb', '0000000000000000' ); return $retVal; } $key = "bf19zWnbPmJxOvzRuP85Bw=="; $data = "q2SYE7hWWltsBw5byuwl/IkGmOOm+94="; echo decryptColdfusionString($key, $data) . PHP_EOL; 
0
source

(I understand that this is a couple of years, but in case someone encounters the same problem ...)

OFB mode requires IV. Although ColdFusion code does not explicitly specify IV, it is still automatically generated and added to the result . This IV must be extracted properly during decryption, or the result will be gibberish. 1. First decode the encrypted base64 string. 2. Then extract the IV and data from the decoded value and decrypt as usual.

PHP:

 $key = base64_decode("bf19zWnbPmJxOvzRuP85Bw=="); $encrypted="q2SYE7hWWltsBw5byuwl/IkGmOOm+94="; $decoded = base64_decode($encrypted); $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_NOFB); $iv = substr($decoded, 0, $ivSize); $data = substr($decoded, $ivSize); $text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_NOFB, $iv); 

Result:

 testing 
0
source

All Articles