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