469,909 Members | 1,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

Need to translate this to C

Greetings++, folks:

I am really desperate on this search. I have been looking
all over for a sample code that uses the GPGme (GPG Made Easy)
SDK library to decrypt a file.

I finally found the code (see below), but it doesn't compile
with gcc. Could you folks translate it to C, please?
I specially need the callback function that handles the
passphrase.

Thanks a lot for sharing your expertise!

-Ramon F Herrera

-----------------------------------------------------------------------

#include <iostream>
#include <iomanip>

#include "gpgme.h"

// Callback function to retrieve the passphrase
const char *myPass(void *hook, const char *desc, void **r_hd)
{
char *sNull = "NULL";
const char *p;
if( desc ) p = desc;
else p = sNull;

std::cout << "myPass(" << p << ")" << std::endl;
static const char *passPhrase = "secret";

if( desc ) p = passPhrase;
else p = NULL;

return p;
}

int main(int argc, char *argv[])
{
GpgmeCtx ctx;
GpgmeData ciphertext, plaintext;
GpgmeRecipients rset;

gpgme_new (&ctx);
gpgme_set_armor (ctx, 1);
gpgme_set_passphrase_cb (ctx, myPass, NULL);

char *plain = "Hallo Welt";

gpgme_data_new_from_mem (&plaintext, plain, strlen(plain), 1 );
std::cout << "gpgme_data_new_from_mem(plaintext) ok" << std::endl;

GpgmeError err = gpgme_data_new ( &ciphertext );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_data_new error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_data_new(cipher) ok" << std::endl;

err = gpgme_recipients_new (&rset);
if( err != GPGME_No_Error )
{
std::cout << "gpgme_recipients_new error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_recipients_new() ok" << std::endl;

err = gpgme_recipients_add_name (rset, "ne***@gmx.de");
if( err != GPGME_No_Error )
{
std::cout << "gpgme_recipients_add_name error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_recipients_add_name() ok" << std::endl;

err = gpgme_op_encrypt (ctx, rset, plaintext, ciphertext );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_op_encrypt error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_op_encrypt() ok" << std::endl;

char buf[4096];
size_t nread;
err = gpgme_data_read( ciphertext, buf, sizeof(buf), &nread );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_data_read error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_data_read() ok " << nread << " bytes" << std::endl;

buf[nread] = 0;
std::cout << buf;

// free all used resources

gpgme_data_release (plaintext);
gpgme_data_release (ciphertext);
gpgme_recipients_release (rset);

//
// Decrypt
//
err = gpgme_data_new_from_mem (&ciphertext, buf, nread, 1 );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_data_new_from_mem(buf) error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_data_new_from_mem(ciphertext) ok" << std::endl;

gpgme_data_new (&plaintext);
err = gpgme_op_decrypt (ctx, ciphertext, plaintext);
if( err != GPGME_No_Error )
{
std::cout << "gpgme_op_decrypt error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_op_decrypt() ok" << std::endl;

gpgme_data_release(ciphertext);

err = gpgme_data_read( plaintext, buf, sizeof(buf), &nread );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_data_read error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_data_read() ok " << nread << " bytes" << std::endl;

buf[nread] = 0;
std::cout << buf;

gpgme_data_release(plaintext);

gpgme_release (ctx);

return 0;
}
Jul 22 '05 #1
3 2637
Ramon F Herrera wrote:
Greetings++, folks:

I am really desperate on this search. I have been looking
all over for a sample code that uses the GPGme (GPG Made Easy)
SDK library to decrypt a file.

I finally found the code (see below), but it doesn't compile
with gcc. Could you folks translate it to C, please?
I specially need the callback function that handles the
passphrase.


Why can't you just use g++, and compile it as c++? Can you for some
reason only use C?

Chris
Jul 22 '05 #2
Ramon F Herrera wrote:
Greetings++, folks:

I am really desperate on this search. I have been looking
all over for a sample code that uses the GPGme (GPG Made Easy)
SDK library to decrypt a file.

I finally found the code (see below), but it doesn't compile
with gcc. Could you folks translate it to C, please?
I specially need the callback function that handles the
passphrase.

Why not use C++ to compile it (GCC handles this) and
declare the myPass function as "extern "C"" if you want to
call it from C ?

Jul 22 '05 #3
Ramon F Herrera wrote:
Greetings++, folks:

I am really desperate on this search. I have been looking
all over for a sample code that uses the GPGme (GPG Made Easy)
SDK library to decrypt a file.

I finally found the code (see below), but it doesn't compile
with gcc. Could you folks translate it to C, please?
I specially need the callback function that handles the
passphrase.

Thanks a lot for sharing your expertise!

-Ramon F Herrera

-----------------------------------------------------------------------

#include <iostream>
#include <iomanip>

#include "gpgme.h"

// Callback function to retrieve the passphrase
const char *myPass(void *hook, const char *desc, void **r_hd)
{
char *sNull = "NULL";
const char *p;
if( desc ) p = desc;
else p = sNull;

std::cout << "myPass(" << p << ")" << std::endl;
static const char *passPhrase = "secret";

if( desc ) p = passPhrase;
else p = NULL;

return p;
}
[put relevant includes here]

char const* myPass(void *hook, char const *desc, void **r_hd)
{
if (desc)
{
printf("myPass(%s)\n", desc); fflush(stdout);
return "secret";
}
else
{
printf("myPass(NULL)\n"); fflush(stdout);
return NULL;
}
}

[...]


V
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by GreyWyvern | last post: by
2 posts views Thread by Joh | last post: by
5 posts views Thread by Mariusz Sakowski | last post: by
6 posts views Thread by bobueland | last post: by
1 post views Thread by peterbe | last post: by
bvdet
9 posts views Thread by bvdet | last post: by
reply views Thread by alaa123 | last post: by
15 posts views Thread by javelin | last post: by
4 posts views Thread by kovariadam | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.