{{ message }}

Instantly share code, notes, and snippets.

# joostrijneveld/gpg2qrcodes.sh

Created May 20, 2014
Producing printable QR codes for persistent storage of GPG private keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # Heavily depends on: # libqrencode (fukuchi.org/works/qrencode/) # paperkey (jabberwocky.com/software/paperkey/) # zbar (zbar.sourceforge.net) # Producing the QR codes: # Split over 4 codes to ensure the data per image is not too large. gpg --export-secret-key KEYIDGOESHERE | paperkey --output-type raw | base64 > temp split temp -n 4 IMG for f in IMG*; do cat $f | qrencode -o$f.png; done # Importing the QR codes: # Note that, when making scans or photographs, you do not produce large images. # If zbar does not recognise your QR code, try downscaling the image. for f in IMG*.png; do zbarimg --raw $f | head -c -1 >$f.out ; done cat *.out | base64 -d | paperkey --pubring ~/.gnupg/pubring.gpg | gpg --import

### costrouc commented Feb 1, 2018

Another alternative is a self generating latex document. Run pdflatex --shell-escape gpgbackup.tex. This will produce a pdf that you can print.

gpgbackup.tex

\documentclass{article}

\usepackage{float}
\usepackage{graphicx}
\usepackage{pgffor}

\def\gpgkey{yourkeyname}

\newcommand\gpgtopng{
\immediate\write18{gpg --export-secret-key \gpgkey | paperkey --output-type raw | base64 > temp}
\immediate\write18{split temp -n 4 IMG}
\immediate\write18{for f in IMG*; do cat $f | qrencode -o /tmp/$f.png; done}
\immediate\write18{rm IMG* temp}
}

\gpgtopng

\begin{document}

\foreach \l in {a, ..., d} {
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{/tmp/IMGa\l.png}
\caption{QR \l}
\end{figure}
}

To recover run the following command

\begin{verbatim}
for f in IMG*.png; do zbarimg --raw $f | head -c -1 >$f.out ; done
cat *.out | base64 -d | paperkey --pubring ~/.gnupg/pubring.gpg | gpg --import
\end{verbatim}

\end{document}

### nurupo commented Aug 3, 2018 • edited

Here is a bash script that converts any file into a printable PDF with QR codes that you can use to restore the file back https://github.com/nurupo/paper-store.

See PDF examples at https://github.com/nurupo/paper-store#examples.

### jgogstad commented Aug 25, 2018 • edited

What's the reason for passing the output from zbarimg through head -c -1? What's up with the last byte? Is it for dropping newlines?

Edit: Yes it is

### elquimista commented Nov 12, 2018

head -c -1 should be just head (without any args) in macOS.

### ShmuelLevine commented Sep 19, 2019

@stueja - This was extremely helpful. The only real suggestion I have is in the following section:

 ### CONVERT TEXT KEYS TO PNG FOR PAGE GENERATION
 # Ref: http://www.imagemagick.org/script/command-line-options.php#family
 echo "Converting textual key parts to png:"
 for f in x*.txt; do convert -family Courier $f txt-$f.png; done

it would be preferable to append TEXT: to the input filename to ensure that imagemagick properly reads the file as a text file. Otherwise, it may not accept the input file, since, by default, .txt files are not interpreted as raw text (at least in the version running on my machine).

Thus, I would suggest amending the line in question to:
for f in x*.txt; do convert -family Courier TEXT:$f txt-$f.png; done

Incidentally, given its usefulness, it seems to me that it's a little unfortunate for your script to be hardcoded to only work for pgp keys, as it is equally useful for storing other data such as SSH keys. If I get a chance, I'll try to cobble together some additional header lines that will allow the script to either use the pgp key as you've done or to accept any text from stdin (so that any key could be piped to it).