For my laravel application, I need the gpg command to do encryption and decryption of CSV file using asymmetric key.
In my laravel application, I am already using symmetric key encryption and decryption using gpg command. But
Below is the command I used for encryption using symmetric key
exec("echo {$key} | gpg --passphrase-fd 0 --batch --yes -o {$filename['enc_name']} --armor --symmetric {$filename['name']}", $output, $retval);
Below is the command I used for decryption using symmetric key
$fd = [ 0 => [ 'pipe', 'r'], 1 => [ 'pipe' , 'w' ] ];
proc_open("gpg --passphrase-fd 0 --batch -o $target -d $filename",
$fd,
$pipes,
Storage::disk('rg-local')->path( '' )
);
if( is_resource($process) )
{
fwrite( $pipes[0], $passphrase );
fclose( $pipes[0] );
$result = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );
$retval = proc_close( $process );
if($retval > 0)
{
Log::alert("Failed to decrypt file $filename. ProcOpen returned $retval. Error: $result");
throw new Exception("Failed to decrypt file $filename. ProcOpen returned $retval. Error: $result");
}
}
For asymmetric encryption/decryption, I removed ‘–symmetric’ option from the above encryption command. But I got error as below (in laravel tinker)
> fwrite( $pipes[0], $passphrase );
gpg: AES256.CFB encrypted data
= 5888
> fclose( $pipes[0] );gpg: encrypted with 1 passphrase
gpg: decryption failed: Bad session key