Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been trying to use the RSA C library on a C++ application (since it seems like the proper way to do it, due to the lack of an "official" [read: OpenSSL] C++ implementation). The thing is, whenever I try to compile a testing code:

#include <iostream>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <unistd.h>
using namespace std;

int main(int argc, char *argv[]) {
    RSA *myKeyPair = RSA_generate_key(2048, 65537, NULL, NULL);
    unsigned char *to;
    unsigned char *totwo;

    if (myKeyPair == NULL) {
        cout << "ERROR 1" << endl;
        return 0;
    }

    to = (unsigned char *) malloc(RSA_size(myKeyPair) - 11);

    if (RSA_private_encrypt(13, (const unsigned char *)"Hello World!", to, myKeyPair, RSA_PKCS1_PADDING) == -1) {
        cout << "ERROR 2" << endl;
        return 0;
    }

    cout << to << endl;

    totwo = (unsigned char *) malloc(RSA_size(myKeyPair) - 11);

    if (RSA_public_decrypt(13, to, totwo, myKeyPair, RSA_PKCS1_PADDING) == -1) {
        cout << "ERROR 3" << endl;
        return 0;
    }

    cout << totwo;

    free(to);
    free(totwo);
    RSA_free(myKeyPair);
    return 0;
}

I get undefined reference errors for all RSA_* functions (please notice that I AM linking with -lssl). To make it even weirder, compiling the very same code using GCC does the job (well, of course, it won't compile because of the C++ std lib references, but the point is that I don't get undefined reference for RSA functions).

Any ideas on how to go about this? (without recurring to any other lib?).

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You might want to take a look at the How to mix C and C++ section of the C++ FAQ.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.