当前位置: 动力学知识库 > 问答 > 编程问答 >

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

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

in that order will fix the problem.

分享给朋友:
您可能感兴趣的文章:
随机阅读: