how to use php call linux GPG encrypt file with a passphase - php

I have a task which need to
use gpg to encrypt the upload file in php
my code is:
("echo '1234' | gpg --passphrase-fd 0 -c /path/aaa.jpg ");
it works by paste the code in linux
but not work by php any solution

You need to use gnupg_decypt() to decrypt the text.

Related

gnupg decrypt command with php with passphrase

Im using Gnupg to decrypt a file:
gpg --decrypt -o file.xml file.gpg
You need a passphrase to unlock the secret key for
user: "TEST-COMPANY (DAM Key) <test#test.de>"
4096-bit RSA key, ID 257C2D21, created 2018-04-23
Enter passphrase:
Then I write this passphrase and then works.
And now I want to make it automatic using this command on PHP:
$command = 'gpg --decrypt -o file.xml file.gpg'
exec($command);
The problem came when system ask for phassphrase.
I tried this:
$command = 'gpg --decrypt -o file.xml file.gpg | [Passphrase]'
but doesn't work.
Any idea about this?
Thank you
Just adding the answer that the OP and #CD001 figured out in the comments, because it helped me immensely (thanks!), and seems like a common issue (secret key was generated with passphrase, and generating new keys isn't an option). I was pulling my hair out trying to decrypt with the GnuPG functions, before learning that as of GnuPG 2.1, it can't decrypt a file with passphrase-generated key (as noted in comment here). Configuring gpg-agent with a preset passphrase may work fine, but I much prefer what the OP here did.
$encrypted_file = "file.csv.pgp";
$path_to_file = $_SERVER["DOCUMENT_ROOT"]."/dir1/dir2";
$passphrase = "passphrase";
$command = "echo {$passphrase} | gpg --passphrase-fd 0 --batch --yes {$path_to_file}/{$encrypted_file}";
exec($command);
If successful, the decrypted file will be in the same directory, without the .pgp extension. So make sure it was successful...
$decrypted_file = str_replace(".pgp", "", $encrypted_file );
if (file_exists("{$path_to_file}/{$decrypted_file}")) {
echo "Successfully decrypted $encrypted_file to $decrypted_file";
}

How to decrypt a GPG file in a PHP script

I'm trying to decrypt a GPG file in a PHP script. I'm trying to decrypt it with GPG tool and I am using the following PHP code:
shell_exec('gpg --batch -r '.$passphrase.' '.$outputFile)
... where $passphrase is password and $outputFile is the file path.
But it's not working. Any suggestions?

How to encrypt a string with GnuPG?

How can I encrypt a given string using gpg from command line?
I have the public key stored in a file called pubkey.pub
I thought I could simply do it with something like that.
gpg --import "path/to/pubkey.pub" --encrypt "my string to encrypt"
But this won't work.
Background: I have to use the PHP exec command to encrypt given text, because I don't have the PHP module itself installed on the server.
gpg reads from stdin while encrypting, thus run
echo "my string to encrypt" | gpg --encrypt
gpg --import imports key material to GnuPG's keystore, where it remains; thus you only have to call it once (and it is a rather slow operation, as it might trigger updating your trust database).

Encrypting with GPG using PHP exec

I try use php exec function to encrypt an given string like
exec("echo test | /usr/local/bin/gpg -e -a -r name#host.local --trust-model always", $output, $encrypted);
echo $encrypted;
That same command works fine in command line and outputs the encrypted message. But for whatever reason I get always 2 as exit code when running in PHP. I figured out that it could be a permission problem. But how to solve it? I read about setting --homedir but without success :(
BIG thanks in advance!
PS: I cannot simply use the gnupg php moduleā€¦

Mcrypt Win32 vs. PHP Mcrypt extension

I'm looking forward encrypting a file using Win32 command-line mcrypt.exe. Then decrypt it using an apache/unix based PHP script.
So I do in win's command-line:
mcrypt -a "blowfish" -k 1234 -m cbc test.txt
(test.txt is a simple text file that contains "working fine"
This generates a 47 bytes files named test.txt.nc
So I upload the file to my apache webserver and run this script:
$s = mcrypt_cbc("blowfish","1234",file_get_contents("test.txt.nc"),MCRYPT_DECRYPT);
file_put_contents("newtext.txt",$s);
I get this warning:
Warning: mcrypt_cbc() [function.mcrypt-cbc]: Attempt to use an empty IV, which is NOT recommend
And a 48 bytes newtext.txt file, which contains binary data instead of the decrypted text file
I need some guidance!. Thank you very much

Categories