gpgme tutorial t-decrypt.c -> Bad passphrase

Mandar Mhatre mandar.mhatre at
Mon Jun 27 14:44:14 CEST 2016

Hello List,

I am a noob to Cryptography world and few weeks ago started into this
subject. Till now I could use linux console freely and perform all the
described operations. Right now I am trying to understand GPGME and going
through the provided tutorial codes. I am getting an error while running
t-decrypt.c (and t-sign.c as well) as stated in the subject line. The
passphrase_cb has been implemented in ‘t-support.h’ but somehow the code
doesn’t retrieve the required password. I tried to implement my own
callback function on the same lines and came to know that this function is
never being called.

Search on internet didn’t provide me any alternative working code nor any
solution for this problem (Apart from John Morris, Sep 7, 2012) . So I am
mailing  GnuPG/GPGME profis.

My goal is to pass a hardcoded password for decryption and signing for
automatic decryption and signing . Any suggestions/modifications in the
code or tutorial codes are appreciated.

Attaching my (tutorial code from GPGME with some debug specific printf’s)
code and other info below.

Thank you in advance.

Mandar Mhatre

System info—

Raspberry pi 3 with installed gpg, gpg2 and GPGME with dependencies


/* t-decrypt.c - Regression test.

   Copyright (C) 2000 Werner Koch (dd9jn)

   Copyright (C) 2001, 2003, 2004 g10 Code GmbH

   This file is part of GPGME.

   GPGME is free software; you can redistribute it and/or modify it

   under the terms of the GNU Lesser General Public License as

   published by the Free Software Foundation; either version 2.1 of

   the License, or (at your option) any later version.

   GPGME is distributed in the hope that it will be useful, but

   WITHOUT ANY WARRANTY; without even the implied warranty of


   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public

   License along with this program; if not, write to the Free Software

   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA

   02111-1307, USA.  */

/* We need to include config.h so that we know whether we are building

   with large file system (LFS) support. */

#define _FILE_OFFSET_BITS 64


#include <config.h>


#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <unistd.h>

#include <gpgme.h>

#include "t-support.h"

gpgme_error_t passphrase_cb1 (void *hook, const char *uid_hint, const char
*passphrase_info, int last_was_bad, int fd)


#ifdef HAVE_W32_system

  DWORD written;

  WriteFile ((HANDLE) fd, "abc\n", 4, &written, 0);


  printf("in passphrase cb");

  int res;

  const char *pass = "abc\n";

  int passlen = strlen (pass);

  int off = 0;



                res = write (fd, &pass[off], passlen - off);

                printf("passphrase is %s", pass);

                if ( res > 0)

                 off += res;


  while(res > 0 && off != passlen);

  return off == passlen ? 0 : gpgme_error_from_errno (errno);


  return 0;



main (int argc, char *argv[])


  gpgme_ctx_t ctx;

  gpgme_error_t err;

  gpgme_data_t in, out;

  gpgme_decrypt_result_t result;

  const char *cipher_1_asc = make_filename ("cipher-1.asc");

  char *agent_info;

  init_gpgme (GPGME_PROTOCOL_OpenPGP);

  err = gpgme_new (&ctx);

  fail_if_err (err);

  err = gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_OpenPGP,

  agent_info = getenv("GPG_AGENT_INFO");

  if (!(agent_info && strchr (agent_info, ':')))


                printf("in if loop\n");

                gpgme_set_passphrase_cb (ctx, passphrase_cb1, NULL);


  err = gpgme_data_new_from_file (&in, cipher_1_asc, 1);

  fail_if_err (err);

  err = gpgme_data_new (&out);

  fail_if_err (err);

  printf("data ready \n");

  err = gpgme_op_decrypt (ctx, in, out);

  fprintf (stderr,"Encrypt error: %s\n", gpgme_strerror (err));

  fail_if_err (err);

  result = gpgme_op_decrypt_result (ctx);

  if (result->unsupported_algorithm)


      fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",

                       __FILE__, __LINE__, result->unsupported_algorithm);

      exit (1);


  print_data (out);

  gpgme_data_release (in);

  gpgme_data_release (out);

  gpgme_release (ctx);

  return 0;



In if loop

Data ready

Encrypt error: Bad passphrase

t-decrypt.c:101: GPGME: Bad passphrase
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/attachments/20160627/765f5b25/attachment-0001.html>

More information about the Gnupg-users mailing list