php将其他行写入加密文件?

时间:2011-06-29 21:33:15

标签: php file encryption xor writing

我正在尝试打开一个加密文件,该文件将存储信息列表,然后添加一个包含信息的新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的完整列表及其相应的组,但由于某种原因,它只显示最后一个输入:(

1 个答案:

答案 0 :(得分:2)

我不会称之为加密。 “谷物盒解码器环”,也许吧。如果要加密,请使用mcrypt功能。充其量这就是混淆。

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

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

按此顺序将解决问题。