Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Assuming I have encrypted a (possibly large) file using GPG; e.g.

gpg --recipient "Some Name" -o this_file.gpg --encrypt this_file.txt

Is it possible to add another recipient without first decrypting the file, followed by another encryption?

share|improve this question
    
You encrypted the file with "Some Name"'s public key. And what shall it use for another recipient? – ott-- Jan 20 '12 at 11:56
6  
@ott: That isn't quite how it works (AFAIK). PGP encrypts the file with a symmetric key not with a recipients public key. Then itseparately encrypts several copies of that symmetric-key using each recipient's public key. – RedGrittyBrick Jan 20 '12 at 14:33
    
@RedGrittyBrick I don't know about pgp, but gpg uses --symmetric for that. With --recipient it uses th recipient's public key. There may be more than 1 recipient, but it must be done in one command, not in 2 separate commands. – ott-- Jan 21 '12 at 17:40
3  
@ott: I read that "It is also possible to encrypt a file simultaneously for any number of multiple recipients by means of a command of the form gpg -e -r <name1> -r <name2> ... <file>" I haven't tried this myself though. It fits with what I learned of crypto many years ago which is that it is almost always more efficient to use fast symmetric algorithms to encrypt the message text. Only the message-key is encrypted using slow asymmetric encryption. – RedGrittyBrick Jan 21 '12 at 17:57
up vote 7 down vote accepted

Short answer: no

First of all, note that if you are not one of the recipients, it is completely impossible. You do not even have the ability to decrypt the file, much less add a recipient. Even if you encrypted it two seconds ago.

Assuming you are a recipient, it is technically possible. The file is actually encrypted with a session key and the session key is encrypted with your public key, so you could in theory decrypt the session key and reencrypt it to another persons' key, and then package everything together in a file just as if you had originally encrypted the document to both people.

However, gpg does not have this capability. The closest you can get with gpg is

  1. Use the --show-session-key option to get the session key (which seems to also decrypt the file, missing the point in this case)
  2. Encrypt that session key to someone else's public key (actually, this creates a new session key and uses that session key to encrypt the original session key)
  3. Send the both files.
  4. The recipient can decrypt the session key and use --override-session-key to decrypt the original message.
share|improve this answer

1) This situation is why encrypting a file to yourself (as well as the intended recipients) is always a good idea. RedGrittyBrick is correct above in describing how GPG and PGP work, which flows into nathang's answer above.

2) However, if you have the original file, you're best off to simply create a new encrypted file to the new recipient.

Assuming you don't want to go the session key route from nathang's suggestion, if you encrypted the file to yourself (as above in #1) in the first place, then decrypt it and then follow step #2 above.

If you neither have the original nor encrypted it to yourself, you cannot get the data back and cannot encrypt it to anyone else without that first recipient sending you back a copy.

share|improve this answer
1  
Actually, it isn't always a good idea. See PGP: Including your public key as a recipient? Any less secure? on the Information Security Stack Exchange for some arguments for and against. – Michael Kjörling May 7 '14 at 7:40

Is it possible to add another recipient without first decrypting the file, followed by another encryption?

It's not possible without decrypting at all but it seems like it should be possible without decrypting to a file:

gpg -d this_file.gpg | gpg -e -r "Some Name" -r "Another Name" -o this_file.gpg

Except this seems to result in a corrupted file (at least sometimes) because gpg is both reading and writing to the same file simultaneously:

Instead, you could do the following (in a script) to avoid needing to manage the decrypted file:

gpg -d -o this_file.txt this_file.gpg

gpg -e \
    -r "Some Name" \
    -r "Another Name" \
    -o this_file.gpg \
    --batch --yes \
    this_file.txt

shred -u this_file.txt

shred, with the -u option, securely overwrites the data of and then deletes the file you specify. Similar programs exist for other platforms, like srm on Mac OS X.

Or you could write your script like this (courtesy of dave_thompson_085) to avoid even needing to store the decrypted data in a file at all:

gpg -d this_file.gpg \
| gpg -e \
    -r "Some Name" \
    -r "Another Name" \
    -o this_file.gpg.new \
    --batch --yes

mv -f this_file.gpg.new this_file.gpg
share|improve this answer
1  
You don't need to write out a clear temp file if you leave the encrypted in place temporarily: gpg -d file.gpg | gpg -e ... -o file.new; mv file.new file.gpg or its mirror mv file.gpg file.old; gpg -d file.old | gpg -e ... -o file.gpg. Or (perhaps too) cleverly just keep the old inode: { rm file.gpg; gpg -d /dev/fd/3; } 3<file.gpg | gpg -e ... -o file.gpg – dave_thompson_085 Jun 23 at 18:11
    
@dave_thompson_085 Interesting – thanks! – Kenny Evitt Jun 23 at 18:13

You must log in to answer this question.