Export and import GPG keys in Windows and WSL

The last week I had to change my computer, so i had to move my whole development environment including my GPG keys that I use with GitHub because I want to continue using them on the new computer. So, in this post I will show you the process I followed to successfully export and import my GPG keys.

GitHub verified commits

Requirements

  • A GPG program, like GnuPG (To install one you can check my previous post).
  • Existent GPG key to export (You can create one following this steps).

Export GPG key

  1. First list your keys by looking for the key to export:

    gpg --list-keys
    
  2. Copy the uid of the key and export it using:

    gpg --export -a $KEY_UID > public.key
    
  3. Your public GPG key was successfully exported, now export your private key using:

    gpg --export-secret-key -a $KEY_UID > private key
    

That’s all, your GPG key was successfully exported and you are ready to import it on your new computer.

Import the key

  1. Before importing the keys we must to change their encoding to UTF-8 without BOM, because by default they are exported using UTF-16 with BOM, to convert them you can use:

    (Get-Content -path public.key) | Set-Content -Encoding UTF8 -Path public.utf8.key
    (Get-Content -path public.key) | Set-Content -Encoding UTF8 -Path private.utf8.key
    
  2. Now we can import them using:

    gpg --import public.utf8.key
    gpg --import private.utf8.key
    
  3. Now you can check your freshly imported keys using:

    gpg --list-keys
    gpg --list-secret-keys
    
  4. That’s all, now you can configure git to sign your commits by default following my previous post