Increasing the protection of your stored PGP key

I recently came across a very good (albeit sort of old) post over at Chris Wellons’ null program blog about increasing the default protections on your stored PGP key. The short hand version is that gpg attempts to protect your PGP key from theft by encrypting it on disk so that if anyone gets access to your secret key file they still don’t immediately have access to your PGP key.

Depending on your version of gpg the defaults might be different but one such default is to use CAST5, BLOWFISH or AES128 as the encryption cipher and your salted passphrase run through 65,536 iterations of SHA1 or RIPEMD-160 for the key. While this isn’t the worst security in the world gpg does offer the ability to modify it and possibly even make it stronger by adjusting the cipher and hash algorithm used as well as the number of hash iterations. From the article here is how you accomplish that:

 gpg --s2k-cipher-algo <cipher algorithm> --s2k-digest-algo <hash algorithm> --s2k-mode <# of times the passphrase is mangled] --s2k-count <# of iterations> --edit-key <key id>

Breaking down the above line here is what is actually happening:

    • –s2k-cipher-algo
      • Changes the cipher algorithm used to encrypt the PGP key on disk
    • –s2k-digest-algo
      • Changes the hash algorithm used to extend/build the passphrase and provide an extra workload to help protect against brute force attacks
    • –s2k-mode
      • Changes the number of times that salt is added to the passphrase
    • –s2k-count
      • Changes the number of hash iterations used
    • –edit-key
      • The PGP key you want to modify the protections on

So for example if you wanted to change the key protections to be AES256 using SHA512 with multiple salt mixings and a very large number of iterations (say 65,000,000) you would issue the following command:

gpg --s2k-cipher-algo AES256 --s2k-digest-algo SHA512 --s2k-mode 3 --s2k-count 65000000 --edit-key <key id>

Once you run the command it will open your key for editing. You then have to change your passphrase (you can simply “change” it to the same original one you already have if you want to keep it) and re-save it for the settings to take effect.

To change the passphrase run


Once you’re done save and exit and your key should be re-encrypted using the new settings.

GnuPG reference documentation on the options above can be found here.

2 thoughts on “Increasing the protection of your stored PGP key

  1. Francois Grieu

    Notice that this only improves security against future leaks of private/secret keys, and if the (possibly changed) passphrase is not used in another private/secret key with low s2kcount that leaks.

    • Tyler Burton

      You are correct. Sorry that I didn’t make that clear in the original post text.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>