Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to run gpg encryption on a file without having to register the keys with the system at all.

Ideally, there would be a way to pass in a path to the key file and have the encryption run on the target file for me.

Is there a way to do this? I'm running CentOS Linux.

share|improve this question
    
You can use the --keyring, --secret-keyring, and --no-default-keyring to change which keyring files GPG uses. –  Wyzard Jul 17 '12 at 3:14
add comment

migrated from stackoverflow.com Jul 19 '12 at 3:22

This question came from our site for professional and enthusiast programmers.

1 Answer

Where there's a will, there's a way...

Save the following as an executable script, calling it whatever you want. (I called it G.)

#!/bin/bash
# Requires pubkey_file as 1st arg
# Will automatically set encrypt recip to first pub key found in keyring
# Passes all additional args to gpg
g=$(mktemp -d) && trap "rm -rf $g" EXIT || exit 255                                                                                                                                
gpg --homedir $g --import "$1" 2>&1 | awk /key.\*imported/ >&2
shift
keyid=$(gpg --homedir $g -k --with-colons 2>/dev/null| awk -F: /^pub/{print\$5})
if [[ -n $keyid ]]; then
  gpg --homedir $g --trust-model always -ver $keyid "$@"
else
  echo "Could not find pubkey in imported keyring" >&2
fi

Here's me using it:

[ryran]$ gpg -k
[ryran]$ G /tmp/barackskey.asc /tmp/hhwchk.orig
gpg: key 6B004035: public key "Barack Obama <bo@devnull>" imported
gpg: using subkey 457D05AC instead of primary key 6B004035
gpg: No trust check due to `--trust-model always' option
gpg: reading from `/tmp/hhwchk.orig'
gpg: writing to `/tmp/hhwchk.orig.gpg'
gpg: ELG-E/AES256 encrypted for: "457D05AC Barack Obama <bo@devnull>"
[ryran]$ 
[ryran]$ gpg -k
[ryran]$ G /tmp/barackskey.asc -ao- /etc/issue
gpg: key 6B004035: public key "Barack Obama <bo@devnull>" imported
gpg: using subkey 457D05AC instead of primary key 6B004035
gpg: No trust check due to `--trust-model always' option
gpg: reading from `/etc/issue'
gpg: writing to `-'
gpg: ELG-E/AES256 encrypted for: "457D05AC Barack Obama <bo@devnull>"
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.12 (GNU/Linux)

hQEOA5S5GiBFfQWsEAP+LTru/pgYPOzMDbc6xPbFUcSjq6qBzKvZ4Smf0P1IKesi
9u7P3zq5njsRKMjdKH71EaRMUTOcrTKzRH0iEoYtVSeWUlR1oHNKZewZUXNknRHu
AWc1SLHxrULisl6CenL0u0glQ8/Q4RNCXxeplqMXdtnocig/VGLLTsY1UKCMgtgE
AJDXinycZpOv1nl9UgW6h8e0ifO/txPTmleW2GRMIRCfs59gNYR13x+oGi1EkL6y
rSLicknLj/uQ1a8d8C/4nDEAtGyhtOYzP5SavTOLPR+jr5NrbCNvmKBOmSXCU1y+
KrZpsOxGdNxoIuGEpYlTOZV//BUHStNLv/Szf+pggtfX0nEB1zdMLzK2p0JcJXIs
Jz9PNmNsHZUr5Zys11xe8wx4X+yjMGHt6kqqoPABau1ETDR7J23vguDKAh68rMX7
2doxof/vKEksB8mzePdIh0cUnZqYWurtm/hX02wszJ5bHKPKjKJwl+YRWSG5YWUY
M4h20w==
=1/2D
-----END PGP MESSAGE-----
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.