64base encoded XML

Associate
Joined
18 Oct 2002
Posts
100
Hi

Wonder if someone could help
I have an 64base encoded XML file that I need to decode

There are 100's of products example below

Need to keep the XML as XML format & simply decode the data

I know a bit of php but I'm stuck on this :(

Is there a PHP fuction that will sort this out

Code:
<productdescription>
  <product>
   	   <item_price>MTkuOTk=</item_price>
    <item_code>U1NUNjY3OA==</item_code>
    <item_weight>Mg==</item_weight>
    <item_title>VGhlIExvc3QgU3ltYm9sIChIYXJkY292ZXIp</item_title>
    <item_category>Ym9va3M=</item_category>
    <productid>MzY1NjI3</productid>
    <manufacturer>RGFuIEJyb3du</manufacturer>
    <manuf_code>MDU5MzA1NDI3WA==</manuf_code>
    <item_on_order_qty>MA==</item_on_order_qty>
    <item_virtual_qty>MA==</item_virtual_qty>
    <item_virtual_message></item_virtual_message>
    <item_image>aHR0cDovL2VjeC5pbWFnZXMtYW1hem9uLmNvbS9pbWFnZXMvSS80MUdCWUZhM1hBTC5fU0w1MDBfQUEyNDBfLmpwZw==</item_image>
  </product>
  <attributes>
    <element>
      <Marketing>VGhlIExvc3QgU3ltYm9sIChIYXJkY292ZXIp</Marketing>
    </element>
  </attributes>
  <specification>
    <spec_element>
      <spec_title>Ym9vaw==</spec_title>
      <spec_description>Ym9vaw==</spec_description>

      <spec_value>YW5vdGhlciBib29r</spec_value>
    </spec_element>
    <spec_element>
      <spec_title>Ym9vaw==</spec_title>
      <spec_description>UGFja2FnZQ==</spec_description>
      <spec_value>dHlwZQ==</spec_value>
    </spec_element>
    <spec_element>
      <spec_title>Ym9vaw==</spec_title>
      <spec_description>VGVjaG5vbG9neQ==</spec_description>
      <spec_value>NDVtbQ==</spec_value>
    </spec_element>
    <spec_element>
      <spec_title>UGh5c2ljYWw=</spec_title>
      <spec_description>Y29sb3Vy</spec_description>
      <spec_value>UmVk</spec_value>
    </spec_element>
  </specification>
</productdescription>
 
You'll need the key to the encryption to decode it, and possibly also the salt that was used too.

Failing that there's probably some brute force jobby you can get from a quick google.
 
PHP:
<?php
// Read XML file
$xml = file_get_contents("slasher.xml");

// Hacked from from
// http://www.webdeveloper.com/forum/archive/index.php/t-68389.html
$result = preg_replace_callback('/(\w+)(=?)(=?)(?![^<]*>)/', "decode", $xml);

// Output
echo htmlentities($result);

// Called above in preg_replace_callback for each match, decodes the base64
// and returns it
function decode($matches)
{
	return base64_decode($matches[0]);
}

I know that regex is horrid, and probably doesn't match everything it should. I've never used it before as I've always considered it pure evil :p, but it seems to work on the above example anyway:

slasher.png
 
Last edited:
PHP:
<?php
// Read XML file
$xml = file_get_contents("slasher.xml");

// Hacked from from
// http://www.webdeveloper.com/forum/archive/index.php/t-68389.html
$result = preg_replace_callback('/(\w+)(=?)(=?)(?![^<]*>)/', "decode", $xml);

// Output
echo htmlentities($result);

// Called above in preg_replace_callback for each match, decodes the base64
// and returns it
function decode($matches)
{
	return base64_decode($matches[0]);
}

I know that regex is horrid, and probably doesn't match everything it should. I've never used it before as I've always considered it pure evil :p, but it seems to work on the above example anyway:

slasher.png

A Big Big thank you
Your a star - Works perfect many many thanks :)
 
One final question is there a way of integrating this to rewrite the XML instead of just returning the decoded data?

Many thanks in advance

Regards

Dave
 
:).

This regular expression might be better than the previous one:
PHP:
// Hacked from from
// http://www.webdeveloper.com/forum/archive/index.php/t-68389.html
$result = preg_replace_callback('/([a-zA-Z0-9]+(=+)?)(?![^<]*>)/', "decode", $xml);


You can use this (below) to scan for all .xml files in the current folder and output them to a decoded directory; you'll need to have made sure this folder exists and you have write permission.

PHP:
<pre>
<?php

// Get all xml files in the current directory
// If using folders on Windows then double backslash it (c:\\users\\)
// or single forward slash (c:/users/)
if ( $files = glob("*.xml") )
{
	// For each xml file
	foreach ($files as $file)
	{
		echo "Reading $file..</br>\n";
		
		// Read XML file contents and decode it
		$xml = decode_xml(file_get_contents($file));
		
		// Decoded file path (puts it inside a decoded folder)
		$decoded_file = pathinfo($file, PATHINFO_DIRNAME)."/decoded/".pathinfo($file, PATHINFO_FILENAME).".".pathinfo($file, PATHINFO_EXTENSION);
		
		file_put_contents($decoded_file, $xml);
		
		// Output XML file to file_decoded.xml
	}
}

// Decodes the XML and returns it
function decode_xml($xml)
{
	// Hacked from from
	// http://www.webdeveloper.com/forum/archive/index.php/t-68389.html
	// and http://search.cpan.org/~gaas/MIME-Base64-3.08/Base64.pm
	return preg_replace_callback('/([a-zA-Z0-9]+(=+)?)(?![^<]*>)/', "_decode", $xml);
}

// Called above in preg_replace_callback for each match, decodes the base64 and returns it
function _decode($matches)
{
	//var_dump($matches);
	return base64_decode($matches[0]);
}
?>

Output:
slasher2.png
 
Last edited:
It's worth nothing that the XML tag contents may contain illegal characters after being decoded, which would result in invalid XML. If this is the case, those characters will need to be escaped after being decoded.
 
Back
Top Bottom