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 file 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.