php向加密文件写入额外的行


php writing additional lines to encrypted files?

我正在尝试打开一个加密文件,该文件将存储一系列信息,然后添加一个包含信息的新ID,并将文件保存回原始加密状态。我的xor/base64函数正在工作,但我在获取文件以保留旧信息时遇到了问题。

以下是我当前使用的内容:

$key = 'some key here';
$id = $_GET['id'];
$group = $_GET['group'];
$file = "groups.log";
$fp = fopen($file, "w+");
$fs = file_get_contents($file);

$filedec = xorstr(base64_decode($fs),$key);
$info = "$id: $group";
$filedec = $filedec . "$info'n";
$reencode = base64_encode(xorstr($filedec,$key));
fwrite($fp, $reencode);
fclose($fp);

function xorstr($str, $key) {
$outText = '';
for($i=0;$i<strlen($str);)
  {
    for($j=0;$j<strlen($key);$j++,$i++)
    {
        $outText .= $str[$i] ^ $key[$j];
    }
  }
  return $outText;
}

?>

它应该保存ID及其相应组的完整列表,但由于某种原因,它只显示最后一个输入:(

我不会称之为加密。"麦片盒子解码环",也许吧。如果需要加密,请使用mcrypt函数。这充其量只是一种混淆。

问题是在执行file_get_contents之前执行fopen()。使用模式w+将文件截断为0字节,作为fopen((调用的一部分。因此,当file_get_contents出现时,您已经删除了原始文件。

$fs = file_get_contents(...);
$fh = fopen(..., 'w+');

按照这个顺序就能解决问题。