I am trying to read a data matrix which I have generated from a private 2096-bit encrypted RSA key.

The key was generated using gpg and printed to PDF with the following command:

gpg --export-secret-key MY_PRIVATE_KEY_ID | paperkey --output-type raw | dmtxwrite -e 8 -f PDF > ~/key.pdf

I then printed key.pdf and scanned it, producing the following jpg file:

enter image description here

I now wanted to use the following command to recreate my private key:

dmtxread /path/to/image.jpg | paperkey --pubring ~/.gnupg/pubring.gpg > my-regenerated-private-key.gpg

However, the command dmtxread prints nothing to stdout and returns 1. I have tried tinkering with the error correction by using -C 10000, reducing the tolerance for rotation by using -q 5, and increasing the canvas size of the image to increase the number of blank pixels to the left and right of the matrix. I also tried with a larger resolution scan (I don't know the DPI, but it was 4MB and the squares were very well defined) and adjusting the contrast tolerance using -t 20 and -t 5, all with the same result (although I had to skip pixels using -S 5 with this large file to get it to finish in < 30 minutes).

I tried using --verbose but nothing was printed to stdout (or any diagnostic file as far as I could tell).

I have also tried the -D option to produce a diagnostic image. I'm not really sure what to make of the result though:

enter image description here

Clearly it's struggling with some of the boundaries, but I'm not sure why or how to make dmtxread more robust.

My next step was going to be attempting to decrypt a dummy file using the recovered private key, but obviously I didn't get that far.

It's disappointing that this is so difficult; if there is no easy way to do this with the command-line libdmtx tools, maybe there is another tool that can do this for me out of the box?

Important: it goes without saying that I would never use this private key for any actual encrypting, now that I've shared it online. You should never share your private key.

  • 1
    If there's a specific reason for the downvote please let me know what it is so I can improve the question. – quant Jan 3 '16 at 7:12
  • I'm struggling with exactly the same issue to read a private key. I have a smaller image, 4x4 instead of yours 6x6, though. According to this dmtx FAQ, > [snip] libdmtx, requires a "quiet zone" to surround every barcode region. If your image is cropped so the Data Matrix symbol is touching or nearly touching the image boundary, this might be preventing a successful scan. I've tried adding more white space to the image using a graphical editor but it didn't help for me. I've also tried converting the image into black and white (just two colors, without – Andrey Feb 8 '16 at 5:00

Looks like, I managed to get data from your image.
I did the following:

  1. Open with GIMP
  2. Colors -> Threshold
  3. Position the slider around center (I don't remember exact value I chose) and click OK
  4. Image -> Canvas Size
  5. Choose percents
  6. Canvas Size: Width: 160 %
  7. Canvas Size: Height: 120 %
  8. Offset: click on "Center"
  9. Click Resize
  10. Tools -> Transform Tools -> Rotate tool
  11. Angle: -0.50 and click Rotate (it's useful to add a vertical guide before doing so)
  12. File -> Export As
  13. Choose file name and click Export and then Export (with all defaults)

Here is the image I got after all of these steps:
fixed scan

Running dmtxread gives an instant result (less than a second):
dmtxread --shrink=2 c8wcN1B.jpg > quant.paperkey
Size is 1428 bytes, two first octets looks like binary paperkey format.
To verify the resulting file, you can download it here.


I'm using dmxtread to decode much simpler data matrix barcodes, 22x22 or 26x26. I've found with these that setting the -S skip pixels parameter helps if set to -S2 (skip every second pixel) but skipping any greater number reduces decodability for my images (photos of a barcode printed on plastic).

dmtxread -n -v -S2 -N1 -D imagename.png 

is a typical command for me where -N says stop looking after you decode one string (as my barcodes only contain one). As you've found a failure to decode returns nothing but a new command prompt line.

Looking at your diagnostic image, I would note that there is little activity down the darker LH and bottom edges - this is where dmtx finds a barcode, then moves to the centre of the space and starts to read pixels. My images tend to have more activity on those dark edges.

Your image looks sharp enough that the -t parameter wouldn't do much and I found this parameter didn't assist with decodability on my images.

I'm assuming your command is intended to get dmtxread to decode, then write the key to the .gpg file you specify. For now I'd remove that and get dmtxread to just output to shell or a simple text file to eliminate that as a potential error source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.