使用3DES解密文件时遇到问题。除了文件末尾的小符号外,文件保持完全相同。我尝试更改文件读取类型,但也无法正常工作。是否可能是因为我改变了IV?
这是我的加密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);
}
答案 0 :(得分:3)
代码中存在多个问题:
该文件应如下所示:
$str = '';
while (!feof($fileBeingVerified)) {
$str .= fread($fileBeingVerified, filesize($file));
}
将加密数据写入文件时,请使用“w”模式打开它,以确保该文件不包含其他内容。
$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);