Need to translate this to C

Discussion in 'C++' started by Ramon F Herrera, Dec 1, 2004.

  1. 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, "");
    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;
    }
     
    Ramon F Herrera, Dec 1, 2004
    #1
    1. Advertising

  2. Ramon F Herrera

    chris Guest

    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
     
    chris, Dec 1, 2004
    #2
    1. Advertising

  3. Ramon F Herrera

    Ron Natalie Guest

    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 ?
     
    Ron Natalie, Dec 1, 2004
    #3
  4. 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
     
    Victor Bazarov, Dec 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar Hegde
    Replies:
    2
    Views:
    2,550
    Ajeetha Kumari
    Sep 13, 2004
  2. Rakesh Sharma
    Replies:
    2
    Views:
    2,958
    Thomas Rudloff
    Oct 22, 2004
  3. Nisheeth

    post translate simulation

    Nisheeth, Apr 29, 2005, in forum: VHDL
    Replies:
    2
    Views:
    864
    Mike Treseler
    Apr 29, 2005
  4. Mariusz Sakowski

    need to translate from Delphi to C++

    Mariusz Sakowski, Dec 13, 2003, in forum: C++
    Replies:
    0
    Views:
    325
    Mariusz Sakowski
    Dec 13, 2003
  5. Mariusz Sakowski

    Need to translate from Delphi to C++

    Mariusz Sakowski, Dec 13, 2003, in forum: C++
    Replies:
    5
    Views:
    444
    Jerry Coffin
    Dec 16, 2003
Loading...

Share This Page