| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Ramon F Herrera" |
| Date: |
01 Dec 2004 11:15:37 AM |
| Object: |
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, "neo42@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;
}
.
|
|
| User: "Ron Natalie" |
|
| Title: Re: Need to translate this to C |
01 Dec 2004 11:26:06 AM |
|
|
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 ?
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Need to translate this to C |
01 Dec 2004 11:27:57 AM |
|
|
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
.
|
|
|
|
| User: "chris" |
|
| Title: Re: Need to translate this to C |
01 Dec 2004 11:23:24 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|