Convert plaintext to QR code on Linux

If you ever need to quickly share plaintext from a computer to a mobile device, generating a QR code can often prove useful and be a more secure method of information transfer in certain situations.

In this post we will

  • Install qrencode to generate QR codes on Debian
  • Generate sample QR codes
  • Create a quick bash script that can be executed whenever we need to generate a QR code from input plaintext (an easy way of sharing website URLs from your computer to mobile device)

Installing qrencode

“Libqrencode is a fast and compact library for encoding data in a QR Code symbol, a 2D symbology that can be scanned by handy terminals such as a mobile phone with CCD. The capacity of QR Code is up to 7000 digits or 4000 characters and has high robustness.” - https://fukuchi.org/works/qrencode/

On Debian based distro’s, install qrencode using apt as follows (or install manually from the URL above):

sudo apt install qrencode

Generating QR codes

To generate a basic QR code with default options from plaintext, run the following command (where ‘Secret text’ is the text we want to convert and ‘qr.png’ is the output QR code image filename):

qrencode -o qr.png 'Secret text'

Additional options (recommended) can be added for more effective QR codes, such as:

-l HIGH # for the highest level of error correction
-s 6 # increase the size of the QR image (customise this value as required)

Resulting in the following command to generate the same QR code as before, but larger and easier to scan and with the highest level of error correction:

qrencode -l HIGH -s 6 -o qr.png 'Secret text'

QR bash script

To save time, we can create a bash script that does the following:

  1. Request input text from the user
  2. Run qrencode against the input text to generate a QR code (filename ‘qr.png’)
  3. Open the image in an image viewer (e.g. gpicview)

In a text editor, save the following text as filename ‘qrgen.sh’:

#!/bin/bash
echo "Enter text to convert to QR:"
read texttoqr
qrencode -l HIGH -s 6 -o qr.png "$texttoqr"
gpicview qr.png

Set as executable by running:

chmod +x qrgen.sh

Run the script to generate a QR code from input text (e.g. “text to convert to qr”):

./qrgen.sh 
Enter text to convert to QR:
text to convert to qr

You can now quickly and easily generate QR codes by running ‘qrgen.sh’ and pasting or typing in any plaintext you wish to convert to QR.

Future posts will cover how attackers may use QR codes for malicious purposes.