473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2799
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.