开发者

OpenSSL - How to determine the right length of an rsa encrypted string?

I use OpenSSL to encryt a string. After that I want to encode the encrypted string with base64 algorithm also using OpenSSL. So I found the following code snipped: ( bit.ly/adUSEw )

char *base64(const unsigned开发者_JAVA技巧 char *input, int length) {
BIO *bmem, *b64;
BUF_MEM *bptr;

b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);

char *buff = (char*)malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length - 1);
buff[bptr->length - 1] = 0;

BIO_free_all(b64);

return buff;
}


int main(int argc, char **argv) {

char *message = "TEST";
char *encryptedString = Encrypt(message);

if (encryptedString == NULL) {
    return 0;
}
else {
    char *output = base64(encryptedString, strlen(encryptedString));
    cout << output << endl;
} }

I noticed that strlen(encryptedString) isn't working properly in this case. Sometimes it returns the right lenght but mostly not. So whats is the proper way to determine the correct lenght?


The size of the encrypted message is exactly the size of the modulus in the private key. You have to get the information from there.

You cannot use strlen because

  • the buffer with the encrypted message is likely not null-terminated, and
  • the encrypted message may contain (and will likely contain) null bytes.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜