Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I encrypt three different files, each with its own public key?

For instance, there are three companies (X, Y and Z). Each company sends me its own public key.

I want to send a file to each company and encrypt it using the correct matching key.

1- encrypt file1.txt by x.pub and send it to company X

2- encrypt file2.txt by y.pub and send it to company Y

3- encrypt file3.txt by z.pub and send it to company Z

How can I do the above in a Windows batch file?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

The problem is that you first have to import the keys in your keyring. After that it's quite easy to automate.

I believe that this should work:

gpg --no-default-keyring --keyring x.gpg --import x.pub
gpg --no-default-keyring --keyring x.gpg --encrypt file1.txt

gpg --no-default-keyring --keyring y.gpg --import y.pub
gpg --no-default-keyring --keyring y.gpg --encrypt file2.txt

gpg --no-default-keyring --keyring z.gpg --import z.pub
gpg --no-default-keyring --keyring z.gpg --encrypt file3.txt

Do note that the import step is only required the first time (assuming that you don't delete the x.gpg files.

share|improve this answer
Looks right to me, three files, three keys. Not a script as such, but does the job! –  Coops Mar 12 '11 at 16:50
when i execute the following command the following error appear :- C:\>"C:\Program Files\GNU\GnuPG\gpg.exe" -r --no-default-keyring --keyring bank_1.gpg --encrypt D:\SSC\keys\port.txt gpg: --no-default-keyring: skipped: public key not found gpg: D:\SSC\keys\port.txt: encryption failed: public key not found –  Mohammad AL-Rawabdeh Mar 13 '11 at 11:04
@Mohammad AL-Rawabdeh: apparently gpg is having problems locating your public key in the keyring. Are you sure the path is correct? Did the --import step run without errors? –  Wolph Mar 13 '11 at 19:12
yes --import run without any error –  Mohammad AL-Rawabdeh Mar 14 '11 at 8:29

Your Answer


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

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