LifeofBob
LifeofBob

Reputation: 37

Decrypting a file not working PHP

I am having a problem while decrypting a file with 3DES. The file stays exactly the same besides a small symbol at the end of the file. I tried changing the file read type and that did not work either. Is it potentially because im changing the IV?

Here is my encrypting php code:

$log = fopen($datalog, 'a') or die("can't open file");

//create a random IV to use with CBC encoding
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
echo " ";
echo "IP: ";
echo $address;
$enc_add = mcrypt_encrypt(MCRYPT_3DES, $key, $address, MCRYPT_MODE_CBC, $iv); 
$add64 = base64_encode($enc_add);
fwrite ($log, $add64);
echo " ";
echo "INFO: ";
echo $info;
$enc_info = mcrypt_encrypt(MCRYPT_3DES, $key, $info, MCRYPT_MODE_CBC, $iv); 
$info64 = base64_encode($enc_info);
fwrite ($log, $info64);
echo " ";
echo "TIMESTAMP: ";
echo $datetimeStamp;
$enc_ts = mcrypt_encrypt(MCRYPT_3DES, $key, $datetimeStamp, MCRYPT_MODE_CBC, $iv); 
$ts64 = base64_encode($enc_ts);
fwrite ($log, $ts64);
echo " ";
echo "COUNTRY: ";
echo $country;
$enc_co = mcrypt_encrypt(MCRYPT_3DES, $key, $country, MCRYPT_MODE_CBC, $iv); 
$country64 = base64_encode($enc_co);
fwrite ($log, $country64);
echo " ";
echo "LATITUDE: ";
echo $lat;
$enc_lat = mcrypt_encrypt(MCRYPT_3DES, $key, $lat, MCRYPT_MODE_CBC, $iv); 
$lat64 = base64_encode($enc_lat);
fwrite ($log, $lat64);
echo " ";
echo "LONGITUDE: ";
echo $long;
$enc_long = mcrypt_encrypt(MCRYPT_3DES, $key, $long, MCRYPT_MODE_CBC, $iv); 
$long64 = base64_encode($enc_long);
fwrite ($log, $long64);

    //data related to ping
$enc_data = mcrypt_encrypt(MCRYPT_3DES, $key, $dataForIP, MCRYPT_MODE_CBC, $iv); 
$data64 = base64_encode($enc_data);
fwrite ($log, $data64);

fclose($log);

}

Here is my code for decrypting:

if ($_POST['submit']) {
    $file = $_POST["filename"];
    $key = $_POST["key"];
    $file_beingVerified = fopen($file, 'a+') or die ("can't open file");

    $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);

    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    while (!feof($file_beingVerified)) {
        $str = fgets($file_beingVerified);

        $reversed = base64_decode($str);
        $enc_add = mcrypt_decrypt(MCRYPT_3DES, $key, $reversed, MCRYPT_MODE_CBC, $iv);

        fwrite($file_beingVerified, $enc_add);
    }
    fclose($file_beingVerified);
}

Upvotes: 1

Views: 331

Answers (1)

lukassteiner
lukassteiner

Reputation: 847

There are multiple issues in the code:

  1. The same IV needs to be used for encryption and decryption.
  2. The file should be read like this:

    $str = '';
    while (!feof($fileBeingVerified)) {
        $str .= fread($fileBeingVerified, filesize($file));
    }
    
  3. When writing the encrypted data to the file, open it with the mode "w" so you can be sure that the file contains no other content.

  4. When reading the file, open it with the mode "r" to place the file pointer to the beginning of the file.
  5. The padding must be removed: $enc_add = rtrim($enc_add, "\0");

See this example for reference:

<?php
$file = '/tmp/data';
$log = fopen($file, 'w') or die("can't open file");

$data = 'hello world';
$ivSize = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$key = 'my-secure-key';

$encData = mcrypt_encrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_CBC, $iv);
$data64 = base64_encode($encData);
fwrite($log, $data64);


$fileBeingVerified = fopen($file, 'r') or die ("can't open file");

$str = '';
while (!feof($fileBeingVerified)) {
    $str .= fread($fileBeingVerified, filesize($file));
}


$reversed = base64_decode($str);
$enc_add = mcrypt_decrypt(MCRYPT_3DES, $key, $reversed, MCRYPT_MODE_CBC, $iv);

$fileBeingVerified = fopen($file, 'w') or die ("can't open file");
fwrite($fileBeingVerified, $enc_add);
fclose($fileBeingVerified);

Upvotes: 3

Related Questions