I have a Ubuntu server and I have installed GnuPG library and it can be visible when I do phpinfo()
also, I tried
if (extension_loaded('gnupg')) {
echo "GnuPG extension is loaded.<br>";
} else {
echo "GnuPG extension is not loaded.<br>";
}
and that confirmed the extension is loaded.
I tried to create a public key using the CLI and it was successful. I could even export the key as a file but when tried to create a public key by running the same command in the PHP script it failed.
$command = "gpg --full-gen-key <<EOF
Key-Type: RSA
Key-Length: 2048
Name-Real: AAA Perera
Name-Email: [email protected]
Expire-Date: 0
%no-protection
%commit
EOF";
// Execute the command
exec($command, $output, $return_var);
echo '<pre>';
print_r($output);
echo '</pre>';
The output is just a blank array.
Then I tried to import the exported key via the CLI into the PHP and encrypt a file, but the import failed, I tried many many times and even tried to get COPILOT help to resolve the case but still couldn’t import the key, or encrypt the file.
Please check the PHP script below and guide me to resolve this issue. Thank you.
putenv("GNUPGHOME=/home/ubuntu/.gnupg");
$gpg = new gnupg();
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
if(file_exists('xxxx-public-key-test1.asc')){
echo 'Yes, file exist <br>';
}else{
echo 'No, file not found <br>';
}
// This out put the file is exist
$keyData = file_get_contents('xxxxx-public-key-test1.asc');
echo $keyData;
// This out put the key file content
$importResult = $gpg->import($keyData);
$errorInfo = $gpg->geterrorinfo();
print_r($errorInfo);
// This out put below error
/*
Array ( [generic_message] => [gpgme_code] => 0 [gpgme_source] => Unspecified source [gpgme_message] => Success )
*/
if ($importResult === false) {
echo "Error importing key: " . $gpg->geterror();
} else {
echo "Key imported successfully.";
}
// This out put only Error importing key but no actual errors