472,371 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 2746
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: GreyWyvern | last post by:
Hello all! I'm in need of an algorithm to change windows-874 (Thai) encoding to UTF-8, without using PHPs iconv or similar functions. I've done the requisite googling and searching through the...
2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
7
by: Bengt Richter | last post by:
Just thought None as the first argument would be both handy and mnemonic, signifying no translation, but allowing easy expression of deleting characters, e.g., s = s.translate(None,...
5
by: Mariusz Sakowski | last post by:
Can someone translate this code to a C++? repeat file.Read(bufor, 1); for p := 1 to KeyCount do begin buf := buf xor Keys; end; targetFile.Write(bufor, 1); until file.Size = file.Position;
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
1
by: peterbe | last post by:
This has always worked fine for me. Peter fine Now if I do it with a unicode string: Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/string.py", line...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
0
by: alaa123 | last post by:
please i really need help to make a start I need to write an XSLT script that takes a file as its input and produces bargraph as its output the file (input) svg:svg...
15
by: javelin | last post by:
I need to be able to create a javascript based drawing/signature box, and be able to save it. Can someone refer me to a script that will allow this, or even a 3rd party package for sale? It can't...
4
by: kovariadam | last post by:
Hi, Does anybody know why i get this error: SQL0176N The second, third or fourth argument of the TRANSLATE scalar function is incorrect. SQLSTATE=42815 with this query: SELECT...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.