I use gpg on ubuntu and I have two keyrings I want to decrypt a message with my second private key but gpg uses the first key for decryption. How I specify private key for decryption? Thanks!

  • 1
    What tool are you using ? gpg on the command line ? see stackoverflow.com/questions/11292196/…
    – Panther
    Apr 12 '17 at 18:47
  • i use gpg built-in from ubuntu yes from command line
    – George
    Apr 12 '17 at 18:49
  • 1
    1) Are you sure that you have two keyrings? 2) When decrypting, the key is usually selected automatically (and correctly) 3) Or do you mean that you intend to encrypt, to create an encrypted file/message?
    – sudodus
    Apr 12 '17 at 18:50
  • See the link I gave you and update your question if that is not a solution.
    – Panther
    Apr 12 '17 at 18:52
  • 1
    What message is it? gpg in its basic way operates with files, encrypts or decrypts a file. Have you created the message to a file in a correct way.
    – sudodus
    Apr 12 '17 at 18:59

You should be able to use --keyring <file> and --no-default-keyring.

From the gpg man page:

   --keyring file
          Add  file to the current list of keyrings. If file begins with a tilde and a slash, these are replaced by the $HOME directory. If the filename does not contain a slash, it is assumed to
          be in the GnuPG home directory ("~/.gnupg" if --homedir or $GNUPGHOME is not used).

          Note that this adds a keyring to the current list. If the intent is to use the specified keyring alone, use --keyring along with --no-default-keyring.

It does seem odd this would be necessary. Perhaps the encrypted file does not specify the recipient properly or at all?
If it is anonymous you might need to use --try-all-secrets also.

  • i will try and i will update you!
    – George
    Apr 12 '17 at 19:26
  • i tried it and uses the second key that is very good but when i enter the passphrase nothing happens and it just tries next key i used this command gpg --try-all-secrets --decrypt yyy.asc (yyy.asc is the file containing the pgp encrypted messsage)
    – George
    Apr 12 '17 at 19:37
  • 2
    The encrypted file is normally expected to have the key id of the keypair needed to decrypt it. It's possible the file was created without this info, or with the wrong ID. I don't see a way to tell gpg which key to use, you can only tell it to try them all.
    – virullius
    Apr 12 '17 at 19:49
  • 1
    AAAH now i know i used the gpg --armor --export command only without specifying the name....my mistake sorry for this guys but i found --try-all-secrets very useful thanks a lot to all!!!
    – George
    Apr 12 '17 at 19:55
  • 1
    I'm hoping the --try-all-secrets suggestion earns me an accepted answer, if it helped solve your problem. :wink:
    – virullius
    Apr 12 '17 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.