Search code examples
encryptiongnupg

decrypt encrypted gpg file using external secret key


I encryptd a file using gpg, now I want to decrypt the file.

Is there any way to decrypt the file without need to import the secret file?

We have the secret key in a file called key.sec; can we pass the secret file to gpg as a parameter (when we run the decrypt command from the bash command line) to use when decrypting the encrypted file? Or must we import the secret key then decrypt the encrypted files?


Solution

  • You must add the secret key to a keyring. From the gpg(1) documentation:

       --no-default-keyring
              Do not add the default keyrings to the list of
              keyrings. Note that GnuPG will not operate without any
              keyrings, so if you use this option and do not provide
              alternate keyrings via --keyring or --secret-keyring,
              then GnuPG will still use the default public or secret
              keyrings.
    

    You could --import --no-default-keyring --secret-keyring temporary to import the key, use --secret-keyring temporary when decrypting the content, then delete the ~/.gnupg/temporary.gpg file when you're done. But that's just a work-around.