473,657 Members | 2,376 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 2823
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
4612
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 in the hopes that I might be able to code the algorithm myself, but I thought I'd ask here as a last...
2
2321
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
2330
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
1942
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
2768
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
9383
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
3096
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. ############################################################################ """ Class...
0
1155
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" font-family="Arial">Monthly Income for Berthold's Cafe</svg:text> <svg:g transform="translate(100, 35)"...
15
4021
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
10643
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 oa.pracovnik p;
0
8399
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6169
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1622
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.