You are not logged in.

#1 2008-12-15 11:55:28

Envil
Member
Registered: 2008-11-18
Posts: 52

[Solved] RSA encryption/decryption in c/c++

Is there any good website with some examples/tutorials or so for rsa in c/c++? So far i didnt find anything decent, only links to Crypto++ or code which deals with key generation etc.
But being kinda noob about cryptography in general this hasnt helped me much so far.

I have a application that receives a 128 byte rsa public key and some encrypted data (in ECB mode or something) which i would like to decrypt, are there any examples specifically for that?
Just encrypting/decrypting data with a key without all the key generation stuff etc.

Last edited by Envil (2008-12-15 21:30:43)

Offline

#2 2008-12-15 17:32:37

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: [Solved] RSA encryption/decryption in c/c++

man 3 rsa

Offline

#3 2008-12-15 20:41:22

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: [Solved] RSA encryption/decryption in c/c++

What library do i need for RSA_public_encrypt()? i found those for RAND_seed() and RSA_generate_key() but i always get undefined reference for the encrypt function.

$ g++ -lgnutls-openssl -lcrypto rsa.cpp

/tmp/ccnLbfxf.o: In function `main':
rsa.cpp:(.text+0x18a): undefined reference to `RSA_public_encrypt(int, unsigned char*, unsigned char*, rsa_st*, int)'
collect2: ld returned 1 exit status


#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/rand.h>
#include <iostream>


void RSA_free(RSA *rsa);

int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);

int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);

int RSA_size(const RSA *rsa);

RSA *RSA_generate_key(int num, unsigned long e, void (*callback)(int,int,void *), void *cb_arg);

void RAND_seed(const void *buf, int num);


int main(void)
{
    RSA        *_RSA                ;
    char         rand_buff[16]            ;
    unsigned char    text[256]    = "Arch Linux"    ;
    int         i = 0                ;

    RAND_seed(rand_buff, 16);    

    _RSA = RSA_generate_key(1024, 65537, NULL, NULL);

    std::cout << text << std::endl;

    std::cout << RSA_size(_RSA) << std::endl;

    i = RSA_public_encrypt(10, text, text, _RSA, RSA_NO_PADDING);

    std::cout << text << std::endl;

    RSA_free(_RSA);

    return 0;
}

Offline

#4 2008-12-15 20:48:10

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: [Solved] RSA encryption/decryption in c/c++

-lssl

Why are you duplicating function prototypes from header files?

Offline

#5 2008-12-15 21:30:16

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: [Solved] RSA encryption/decryption in c/c++

tam1138 wrote:

-lssl

Why are you duplicating function prototypes from header files?

It kinda looked like having to declare them from the man page (atleast to a noob like me) ^^
After removing the function declarations i dont even need the -lssl switch.
Ty for putting up with my noobness, finally got this to work (and alongside figured out why i wont decrypt the data mentioned earlier with just the public key) =P

Offline

Board footer

Powered by FluxBB