473,560 Members | 3,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_passp hrase_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(plain text) 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_recipient s_new (&rset);
if( err != GPGME_No_Error )
{
std::cout << "gpgme_recipien ts_new error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_recipien ts_new() ok" << std::endl;

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

err = gpgme_op_encryp t (ctx, rset, plaintext, ciphertext );
if( err != GPGME_No_Error )
{
std::cout << "gpgme_op_encry pt error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_op_encry pt() 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_rea d error " << err << std::endl;
exit(0);
}
std::cout << "gpgme_data_rea d() ok " << nread << " bytes" << std::endl;

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

// free all used resources

gpgme_data_rele ase (plaintext);
gpgme_data_rele ase (ciphertext);
gpgme_recipient s_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(ciphe rtext) ok" << std::endl;

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

gpgme_data_rele ase(ciphertext) ;

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

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

gpgme_data_rele ase(plaintext);

gpgme_release (ctx);

return 0;
}
Jul 22 '05 #1
3 2814
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
4572
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 notes on php.net but nothing (that I could see) has turned up. Right now I'll be looking for pages explaining the inner workings of windows-874...
2
2315
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 advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
7
2323
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, 'badcharshere') Regards, Bengt Richter
5
1935
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
2750
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
9374
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 493, in translate return s.translate(table, deletions) TypeError: translate() takes exactly one argument (2 given)
9
3091
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 Bruce Vaughan, BV Detailing & Design, Inc. ## All rights reserved. ## NOT FOR SALE. The software is provided "as is" without any warranty....
0
1148
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 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:svg="http://www.w3.org/2000/svg" width="555" height="320"> <svg:text x="277.5" y="20" text-anchor="middle"...
15
4016
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 be Java or ActiveX. Thank you.
4
10627
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 TRANSLATE(p.meno, 'aaaaccdeeeiillnnoooorrsstuuuuyzzAAAACCDEEEIILLNNOOOORRSSTUUUUYZZ', 'ťئݬ') FROM...
0
7636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7849
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8070
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6194
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5176
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
882
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.