Here is the main part of my gnupg encryption function for encrypting text contained in the '$plain_text' variable and outputting it to the '$encrypted_text' variable:
Code: Select all
$command = "$gpg -a --recipient " . ""$public_key_id"" . " --encrypt";
$pipe=popen($command, "w");
$no_ofbytes_written=fwrite($pipe, escapeshellarg($plain_text));
$encrypted_text = '';
while($s = fgets($pipe, 1024)) {
$encrypted_text .= $s;
}
pclose($pipe);
return $encrypted_text;
I have checked the code and I know that a number of bytes is written to the pipe (the '$plain_text' variable content is written in the pipe, since I get non-zero return from the fwrite() function - $no_ofbytes_written<>0). SO, I guess the encryption works ok. The problem is that I cannot read from the pipe with the fgets() function, since it has been opened with the "w" argument. If I try to open it for reading (with "r" argument), the 'fgets()' function writes zero bytes to it . Therefore, I need to find a way to re-open the same pipe for reading (and of course keep its contents) (i dont want to use physical files since they are insecure for pgp encryption of sensitive data).
Can anybody help me to that ??? In a few words, how can you open a pipe for writing, write to it, and then read from it ??