0

I'm using openssl libraryes on client/server application, and i need to share public key with client. this is the struct of packet:

typedef struct pack {
    char op[10];
    char message[1024];
    int id;
}packet;

and this is how extract the public key and put it in message field:

packet *send_pack
RSA *rsa,
const unsigned char *my_public_key;

my_public_key = malloc(1024);
send_pack = (packet*) malloc(sizeof (packet));
rsa = RSA_new();
rsa = RSA_generate_key(1024, 17, NULL, NULL);
i2d_RSAPublicKey(rsa, &my_public_key);
printf("my public key: %X\n", (unsigned int)my_public_key);
strcpy(send_pack->message, my_public_key);
printf("field message of send pack %X", (unsigned int)send_pack->message );

...

but the two printf print two similar value, but not the same (for example the first printf return A0B9092 and the second A0B944 ). What's wrong ?

2

1 Answer 1

2

There are multiple issues:

  • You're printing pointer addresses instead of contents
  • You're copying the key wrong since strcpy expects 0-terminated strings

For the first issue, you should note i2d_RSAPublicKey returns a length which you're ignoring. So you should print:

int len = i2d_RSAPublicKey(...);
for (i = 0; i < len; i++)
    printf("%02x", my_public_key[i]);

For the second part, use memcpy instead:

memcpy(send_pack->message, my_public_key, len);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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