2015-11-16 69 views
1

我在使用3DES解密文件時遇到問題。除了文件末尾的小符號之外,文件保持完全相同。我試圖改變文件讀取類型,但也沒有工作。這是否可能是因爲我改變了IV?解密文件無法正常工作PHP

這裏是我的加密的PHP代碼:

$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); 

} 

這裏是我的解密代碼:

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); 
} 
+3

解密,你在一個'+'模式打開文件 - ** **追加。 RTFM:http://php.net/manual/en/function.fopen.php''a +'\t適合閱讀和寫作;將文件指針放在文件末尾。「因此,當您讀取數據時,您正在從文件末尾讀取數據。 –

回答

3

有在代碼中的多個問題:

  1. same IV需求用於加密和解密。
  2. 該文件應該這樣寫:

    $str = ''; 
    while (!feof($fileBeingVerified)) { 
        $str .= fread($fileBeingVerified, filesize($file)); 
    } 
    
  3. 當寫加密的數據文件,用模式「W」,所以你可以肯定的是,文件不包含其他內容打開它。

  4. 當讀取文件時,用模式「r」打開它,將文件指針放到文件的開頭。
  5. 的填充必須拆除:$enc_add = rtrim($enc_add, "\0");

參考見這個例子:

<?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);