Upgraded 2017-12-04. (Adding --batch in order to prevent passphrase prompt)
You may have to add --batch
option:
And. if you use recipient key pair you may have to add --pinentry-mode loopback
too.
From version 2 of GPG
, the option --batch
is needed to ensure no prompt...
Ok, looking that:
$ gpg --version
gpg (GnuPG) 2.1.18
libgcrypt 1.7.6-beta
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: /home/user /.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
Trying:
$ newdir=$(mktemp -d)
$ cd $newdir
$ seq 1 10 | gpg -c --batch --passphrase 1234 -o file.gpg -
$ ls -ltr
total 4
-rw-r--r-- 1 user user 91 Dec 4 15:42 file.gpg
$ hd file.gpg
00000000 8c 0d 04 07 03 02 ea fa d0 d3 2b 9a ea 06 df d2 |..........+.....|
00000010 4a 01 ed 50 74 ff 27 45 0e 6c 94 74 db e9 8a a5 |J..Pt.'E.l.t....|
00000020 03 9f 67 a0 73 97 e9 15 6b 56 a0 f0 88 71 85 a8 |..g.s...kV...q..|
00000030 dc 41 71 9f fa 3b f9 9d af ac 80 eb f4 f7 28 19 |.Aq..;........(.|
00000040 9f be 75 47 e6 d8 00 3e f6 60 f1 00 5e 63 57 ef |..uG...>.`..^cW.|
00000050 14 c3 4b 20 ff 94 03 03 c1 fc 98 |..K .......|
0000005b
sound good! Well, now:
$ gpg -d --batch --passphrase 1234 file.gpg
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
1
2
3
4
5
6
7
8
9
10
If no -d
parameter is given (same syntaxe as SO's question), decrypted datas from file.gpg
will be extracted to a new file
:
$ gpg --batch --passphrase 1234 file.gpg
gpg: WARNING: no command supplied. Trying to guess what you mean ...
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
$ ls -ltr
total 8
-rw-r--r-- 1 user user 91 Dec 4 15:42 file.gpg
-rw-r--r-- 1 user user 21 Dec 4 15:44 file
$ cat file
1
2
3
4
5
6
7
8
9
10
This work well!
$ cd -
$ rm -fR $newdir
$ unset newdir
Full sample recipient keyfile:
First create a temporary environment
newdir=$(mktemp -d)
cd $newdir
export GNUPGHOME=$newdir
echo YourPassword >password.txt
gpgconf --kill gpg-agent # Required, if agent_genkey fail...
gpg --generate-key --batch <<eoGpgConf
%echo Started!
Key-Type: default
Key-Length: default
Subkey-Type: default
Name-Real: Full Name There
Name-Comment: Something funny
Name-Email: [email protected]
Expire-Date: 0
Passphrase: $(<password.txt)
%commit
%echo Done.
eoGpgConf
gpg: keybox '/tmp/tmp.xU5Ldyr4iB/pubring.kbx' created
gpg: Started!
gpg: agent_genkey failed: No such file or directory
gpg: key generation failed: No such file or directory
gpg: Done.
Hmm.
gpgconf --kill gpg-agent
gpg --generate-key --batch <<eoGpgConf
%echo Started!
...
eoGpgConf
gpg: Started!
gpg: key 43E6B96CAFABDEDF marked as ultimately trusted
gpg: directory '/tmp/tmp.xU5Ldyr4iB/openpgp-revocs.d' created
gpg: revocation certificate stored as '/tmp/tmp.xU5Ldyr4iB/openpgp-revocs.d/DF223E1612CF917DC3BD42AA43E6B96CAFABDEDF.rev'
gpg: Done.
Get Key ID
Then now
gpg -k
/tmp/tmp.xU5Ldyr4iB/pubring.kbx
-------------------------------
pub rsa3072 2020-06-19 [SC]
DF223E1612CF917DC3BD42AA43E6B96CAFABDEDF
uid [ultimate] Full Name There (Something funny) <[email protected]>
sub rsa3072 2020-06-19 [E]
( The last 8char from pub fingerprint could be used as key alias. )
gpg -k [email protected]| sed -e '/^pub/{N;s/.*\(.\{16\}\)/\1/;p;s/^.\{8\}//;q};d'
43E6B96CAFABDEDF
AFABDEDF
Or even retrieving into a bash variable, using --with-colons
.
while IFS=: read -r typeOfRec _ _ _ keyId _; do
case $typeOfRec in pub ) break ;; esac
done < <(gpg --with-colons -k [email protected] )
declare -p keyId
declare -- keyId="43E6B96CAFABDEDF"
Encrypt
Ok, now!
seq -f "%'8g" 990 5 1015 |
gpg --batch --armor --recipient "$keyId" --encrypt --output file.gpg
Will give something like:
cat file.gpg
-----BEGIN PGP MESSAGE-----
hQEOA5BNpEVKPGsfEAP/XutJp7ME3I1MqG0vZyIS8w+npPQMPicIpQUwM4OVO1rX
2lhrymp0zGqxAH7s9Dh9YJNRA/9zYCO4/vghtnnl/zg10vILs9btgLXY+aupgoQ9
nifnVC8JJ1DC+hZZrIHyzS73BsjufWhpbwURYc7EgIMGKu2TRiy5I8+0aZ4zAtID
/ApL0sTBQ9hqmIatzaYbX9ajmDf1vvtE2/s3MUFA/hIqew2MVMhlb4RjyT7ix03P
LmCH2Mfy88VGr59eSUoZq+CPMDSZpXxbE2LfyPHYsObraO+a6FdVHhj2xcw/tnDO
TcNHTKnTRJSb9sfLAtJmE9eaxebkl27T+UvqyJUG4dgu0lABadboNaEidlrCYLNi
icR19UX0G7E50+i3iKvw0u81YtciYyOnpHvgazb5QbqJNN5P8izC4J3FqW7HaTDI
xnf+8IaX2Vqrq5+k4qLR7h5Vcw==
=1fb5
-----END PGP MESSAGE-----
Note: From version >=2.2
of gpg``--batch
is not required anymore:
seq -f "%'8g" 990 5 1015 |
gpg -aer "$keyId" >file.gpg
Will do near same effect.
Decrypt
Then
gpg --decrypt --pinentry-mode loopback --passphrase-file password.txt --batch file.gpg
or
gpg -d --pinentry-mode loopback --passphrase-file password.txt --batch file.gpg
will produce:
gpg: encrypted with 3072-bit RSA key, ID 58020687E0746339, created 2020-06-19
"Full Name There (Something funny) <[email protected]>"
990
995
1'000
1'005
1'010
1'015
But, from version >=2.2
of gpg
, you could use:
gpg -qd --passphrase "$(<password.txt)" file.gpg
990
995
1'000
1'005
1'010
1'015
gpg
run the right command, not an alias nor a wrapper? Try/usr/bin/gpg --passphrase 1234 file.gpg
,type gpg
,gpg --version
andset | grep '^.\{0,9\}PG'
gpg --list-packets --batch myFile.gpg
prompts for a passphrase, while it doesn't in GPG 1.x. That was my problem (in a program that I'm writing), while I thought I had your problem (the --list-packets thing executed first, before attempting to decrypt, and I didn't notice). So, I made a new way to determine if files were encrypted.gpg --version
2.x (as in Ubuntu 18.04), jump to Xen2050's answer: unix.stackexchange.com/a/415064/237055