我使用OpenSSL加密字符串。之后,我还想使用OpenSSL使用base64算法对加密的字符串进行编码。因此,我发现以下代码已被删除:(bit.ly/adUSEw)

char *base64(const unsigned 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;
} }

我注意到在这种情况下strlen(encryptedString)无法正常工作。有时它返回正确的长度,但大多数情况下不返回。那么确定正确长度的正确方法是什么?

最佳答案

加密消息的大小恰好是私钥中模数的大小。您必须从那里获取信息。

您不能使用strlen,因为

  • 带有加密消息的缓冲区很可能不是空终止的,并且
  • 加密的消息可能包含(并且可能包含)空字节。
  • 关于c++ - OpenSSL-如何确定rsa加密字符串的正确长度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4122066/

    10-14 11:00