开发者

php writing additional lines to encrypted files?

I'm trying to open an encrypted file that will store a list of information, then add a new ID with information, and save the file back as it was originally encrypted. I have xor/base64 functions that are working, but I am having trouble getting the fil开发者_JS百科e to retain old information.

here is what I am currently using:

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


?>

It should save an entire list of the ID's and their corresponding groups, but for some reason it's only showing the last input :(


I wouldn't call this encryption. "cereal box decoder ring", maybe. If you want encryption, then use the mcrypt functions. At best this is obfuscation.

The problem is that you're doing fopen() before doing file_get_contents. Using mode w+ truncates the file to 0-bytes as part of the fopen() call. So by the time file_get_contents comes up, you've deleted the original file.

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

in that order will fix the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜