Page 1 of 1

Encryption/Decryption giving slightly different results

Posted: Thu May 17, 2012 6:04 am
by SweetStick
Good day, I wrote a small class that takes a map file and encrypts it so it can be passed onto a client. Everything works fine until it is decrypted, which gives 99% accurate results, but there are some anomalies. It worked fine on my localhost, but when moved onto a server it stopped working.

Code to encrypt the file:

Code: Select all

<?php
// Set the key
$key = 'b836rbJd0Yh39rfhnJsd03hBd';


// Encrypt
function encrypt($content, $key) {
    $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
    $iv     = mcrypt_create_iv($ivSize, MCRYPT_RAND); 
    return    mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $content, MCRYPT_MODE_ECB, $iv); 
}

// decrypt
function decrypt($content, $key) {
    $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
    $iv     = mcrypt_create_iv($ivSize, MCRYPT_RAND); 
    return    mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $content, MCRYPT_MODE_ECB, $iv);
}


// Start the headers and then force the download
// Set the page as a standard stream
header('Content-Type: application/octet-stream');
// Set the name of the file
header('Content-Disposition: attachment; filename="map.txt"');
// IE7 requires this header
header('Pragma: public');
// Remove cache
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);

// Echo the data onto the page
echo encrypt($_POST['map_content'], $key);
Code to decrypt the file:

Code: Select all

<?php
class Map
{
    /**
     * Get a map from the database. If it is not in the database then we need to decrypt it.
     *
     * @return array
     */
    public function getMap() {
        return parse_ini_string(Map::getDecryptedMap());
    }
    
    /**
     * Get the decrypted version of the map file.
     *
     * @return string
     */
    public function getDecryptedMap() {
        // Get the decryption key from TMT
        $key = 'b836rbJd0Yh39rfhnJsd03hBd';

        // Now decrypt the map
        return Map::decrypt(
            file_get_contents($base . 'map.txt'),
            $key
        );
    }
    
    /**
     * Encrypts a string with a given key.
     *
     * @param string $content
     * @param string $key
     * @return string
     */
    public function encrypt($content, $key) {
        $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
        $iv     = mcrypt_create_iv($ivSize, MCRYPT_RAND); 
        return    mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $content, MCRYPT_MODE_ECB, $iv); 
    }
    
    /**
     * Decrypts a string with a given key.
     *
     * @param string $content
     * @param string $key
     * @return string
     */
    public function decrypt($content, $key) {
        $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
        $iv     = mcrypt_create_iv($ivSize, MCRYPT_RAND); 
        return    mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $content, MCRYPT_MODE_ECB, $iv);
    }
}
And an example of what is returned when it has been decrypted, with an anomaly:

[text]; Place SQL statement below:
sql_orders = "SELECT `order_id`, `name`, H<4ŠÃYí®ë»6b„?1}$S¡½Îs FROM `orders`"[/text]

Re: Encryption/Decryption giving slightly different results

Posted: Fri May 18, 2012 3:04 am
by SweetStick
Anyone have any idea why this might be happening?