├── test.sh ├── LICENSE ├── qr2asc.sh ├── asc2qr.sh └── README.md /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -eq 0 ]; then 4 | echo "usage: $(basename ${0}) " 5 | exit 1 6 | fi 7 | 8 | set -x 9 | 10 | asc_key="$1" 11 | 12 | ./asc2qr.sh "${asc_key}" 13 | 14 | ./qr2asc.sh QR*.png 15 | 16 | diff "${asc_key}" "./mykey.asc" 17 | if [ $? -eq 0 ]; then 18 | echo "Diff Test: PASS" 19 | else 20 | echo "Diff Test: FAIL" 21 | fi 22 | 23 | rm ./QR*.png 24 | rm ./mykey.asc 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kevin Douglas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /qr2asc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ##### 4 | # 5 | # Author: Kevin Douglas 6 | # 7 | # Simple command line script to restore ascii armor gpg keys from a QR image. 8 | # You can use the following commands to import your restored keys: 9 | # 10 | # gpg --import pgp-public-keys.asc 11 | # gpg --import pgp-private-keys.asc 12 | # 13 | # This script will allow you to convert QR images created with asc2qr.sh 14 | # info an ascii armor pgp key. 15 | # 16 | # This script depends on the following libraries/applications: 17 | # 18 | # libqrencode (http://fukuchi.org/works/qrencode/) 19 | # zbar (http://zbar.sourceforge.net) 20 | # 21 | # If you need to backup or restore binary keys, see this link to get started: 22 | # 23 | # https://gist.github.com/joostrijneveld/59ab61faa21910c8434c#file-gpg2qrcodes-sh 24 | # 25 | ##### 26 | 27 | # Name of the output key after decoding 28 | output_key_name="mykey.asc" 29 | 30 | # Argument/usage check 31 | if [ $# -lt 1 ]; then 32 | echo "usage: `basename ${0}` [QR image 2] [...]" 33 | exit 1 34 | fi 35 | 36 | # For each image on the command line, decode it into text 37 | chunks=() 38 | index=1 39 | for img in "$@"; do 40 | if [ ! -f ${img} ]; then 41 | echo "image file not found: ${img}" 42 | exit 1 43 | fi 44 | asc_key="${tmp_file}.${index}" 45 | echo "decoding ${img}" 46 | chunk=$( zbarimg --raw --set disable --set qrcode.enable ${img} 2>/dev/null ) 47 | if [ $? -ne 0 ]; then 48 | echo "failed to decode QR image" 49 | exit 2 50 | fi 51 | chunks+=("${chunk}") 52 | index=$((index+1)) 53 | done 54 | 55 | asc_key="" 56 | for c in "${chunks[@]}"; do 57 | asc_key+="${c}" 58 | done 59 | 60 | echo "creating ${output_key_name}" 61 | echo "${asc_key}" > ${output_key_name} 62 | -------------------------------------------------------------------------------- /asc2qr.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ##### 4 | # 5 | # Author: Kevin Douglas 6 | # 7 | # Simple command line script to backup ascii armor gpg keys to paper. You can 8 | # use the following commands to export your keys in ascii armor format: 9 | # 10 | # gpg --armor --export > pgp-public-keys.asc 11 | # gpg --armor --export-secret-keys > pgp-private-keys.asc 12 | # gpg --armor --gen-revoke [your key ID] > pgp-revocation.asc 13 | # 14 | # These can then be used to restore your keys if necessary. 15 | # 16 | # This script will allow you to convert the above ascii armor keys into a 17 | # printable QR code for long-term archival. 18 | # 19 | # This script depends on the following libraries/applications: 20 | # 21 | # libqrencode (http://fukuchi.org/works/qrencode/) 22 | # 23 | # If you need to backup or restore binary keys, see this link to get started: 24 | # 25 | # https://gist.github.com/joostrijneveld/59ab61faa21910c8434c#file-gpg2qrcodes-sh 26 | # 27 | ##### 28 | 29 | # Maximum chuck size to send to the QR encoder. QR version 40 supports 30 | # 2,953 bytes of storage. 31 | max_qr_bytes=2800 32 | 33 | # Prefix string for the PNG images that are produced 34 | image_prefix="QR" 35 | 36 | # Argument/usage check 37 | if [ $# -ne 1 ]; then 38 | echo "usage: `basename ${0}` " 39 | exit 1 40 | fi 41 | 42 | asc_key=${1} 43 | if [ ! -f ${asc_key} ]; then 44 | echo "key file not found: ${asc_key}" 45 | exit 1 46 | fi 47 | 48 | ## Split the key file into usable chunks that the QR encoder can consume 49 | chunks=() 50 | while true; do 51 | IFS= read -r -d'\0' -n ${max_qr_bytes} s 52 | if [ ${#s} -gt 0 ]; then 53 | chunks+=("${s}") 54 | else 55 | break 56 | fi 57 | done < ${asc_key} 58 | 59 | ## For each chunk, encode it into a qr image 60 | index=1 61 | for c in "${chunks[@]}"; do 62 | img="${image_prefix}${index}.png" 63 | echo "generating ${img}" 64 | echo -n "${c}" | qrencode -o ${img} 65 | if [ $? -ne 0 ]; then 66 | echo "failed to encode image" 67 | exit 2 68 | fi 69 | index=$((index+1)) 70 | done 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easy Paper Backups of Ascii PGP Keys 2 | 3 | Shell scripts to convert between ascii armor PGP keys and QR codes for paper backup. 4 | 5 | After exporting your private keys in ascii armor format, you can use the scripts 6 | in this project to convert them to PNG images that can be printed and archived. 7 | 8 | This is ever so slightly easier than printing the ascii key directly and using OCR 9 | software to recreate the key files. 10 | 11 | ## Dependencies 12 | 13 | This project depends on a couple libraries that come with applicatons that are called 14 | by these scripts. 15 | 16 | 1. [libqrencode](http://fukuchi.org/works/qrencode/) 17 | 2. [zbar](http://zbar.sourceforge.net) 18 | 19 | ## Export keys From GPG 20 | 21 | There are several good guides on the web about managing GPG keys and creating backups. 22 | The quick version is to use one of these commands: 23 | 24 | gpg --armor --export > pgp-public-keys.asc 25 | gpg --armor --export-secret-keys > pgp-private-keys.asc 26 | gpg --armor --gen-revoke [your key ID] > pgp-revocation.asc 27 | 28 | **NOTE** Be sure to securely remove your private and revocation keys once they 29 | are correctly backed up. This can be done from the command line with either the 'srm' 30 | tool on Max OS X or with 'shred' on Linux. 31 | 32 | ## Convert To QR Code Images 33 | 34 | Use the asc2qr.sh script to convert a public or private key in ascii armor format 35 | into QR code PNG images. 36 | 37 | [kevin@computer]$ asc2qr.sh ~/gpg_public_key.asc 38 | generating QR1.png 39 | generating QR2.png 40 | 41 | [kevin@computer]$ ls -l 42 | total 24 43 | -rw-r--r-- 1 kevin group 6873 Mar 7 11:30 QR1.png 44 | -rw-r--r-- 1 kevin group 1251 Mar 7 11:30 QR2.png 45 | 46 | Print the resulting images and save them in a fireproof safe or safety deposit box. 47 | 48 | ## Convert To Ascii Armor Key Files 49 | 50 | Use the qr2asc.sh script to convert QR code images (created from an photo of 51 | the image) to a public or private key in ascii armor format. 52 | 53 | [kevin@computer]$ qr2asc.sh *.png 54 | decoding QR1.png 55 | decoding QR2.png 56 | 57 | [kevin@computer]$ ls -l 58 | total 32 59 | -rw-r--r-- 1 kevin group 3127 Mar 7 11:30 mykey.asc 60 | 61 | [kevin@computer]$ diff ~/gpg_public_key.asc mykey.asc 62 | [kevin@computer]$ 63 | 64 | ## Import Keys Into GPG 65 | 66 | To import keys into GPG use one of these commands: 67 | 68 | gpg --import pgp-public-keys.asc 69 | gpg --import pgp-private-keys.asc 70 | 71 | **NOTE** Be sure to securely remove your private and revocation keys once they 72 | are correctly backed up. This can be done from the command line with either the 'srm' 73 | tool on Max OS X or with 'shred' on Linux. 74 | --------------------------------------------------------------------------------