decrypt mail archive

Klaus Ethgen klaus+gnupg at
Wed Jun 23 12:55:05 CEST 2021


I am experience with imapfilter to decrypt old mail archives but keeping

The reason is that I lost already access to old mails that was encrypted
to a key which is only on a, now broken, GPG card. As the relevant
mailserver is mine and the file system is encrypted, I can accept to
have that mails unencrypted in storage.

I have the following approach at the moment:
   for _, msg in ipairs(result) do
      mbox, uid = unpack(msg)
      header = mbox[uid]:fetch_header()
      body = mbox[uid]:fetch_body()
      flags = mbox[uid]:fetch_flags()
      date = mbox[uid]:fetch_date()

      if (body:match("BEGIN PGP MESSAGE")) then
	 obody = body:match('(%-%-%-%-%-BEGIN PGP MESSAGE%-%-%-%-%-.*%-%-%-%-%-END PGP MESSAGE%-%-%-%-%-\r\n)')
	 state, nbody = pipe_single(obody, "gpg", "--decrypt", "--unwrap")
	 if (state ~= 0) then
	    print("Error "..state)
	 state, nbody = pipe_single(nbody, "gpg", "--enarmor")
	 if (state ~= 0) then
	    print("Error "..state)
	 nbody = nbody:gsub('ARMORED FILE', 'MESSAGE')
	 body = body:gsub('(%-%-%-%-%-BEGIN PGP MESSAGE%-%-%-%-%-.*%-%-%-%-%-END PGP MESSAGE%-%-%-%-%-\r\n)', nbody)

	 message = header .. body
	 privat['sent-mail']:append_message(message, flags, date)

But that do not work in mutt as the signed mail must be in separate mime
parts for text and for signature. But `gpg --unwrap` generate a PGP
binary file with just encryption removed.  Thunderbird works just fine
but mutt doesn't.

Do anybody have any idea how to convert the binary package back to
detached signature and eventually how to build mime parts around in lua?

Klaus Ethgen                             
pub  4096R/4E20AF1C 2011-05-16            Klaus Ethgen <Klaus at>
Fingerprint: 85D4 CA42 952C 949B 1753  62B3 79D0 B06F 4E20 AF1C
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 688 bytes
Desc: not available
URL: <>

More information about the Gnupg-users mailing list