rsa_public_encrypt returns -1,错误0x0406B07A

rsa_public_encrypt returns -1, error 0x0406B07A

我正在尝试使用 RSA_public_encrypt 加密数据,但它似乎不起作用(retEnc 始终为 -1)。我还尝试使用 ERR_get_errorERR_error_string.

查找有关错误的更多信息

这是代码:

RSA *rsaPkey = NULL;

FILE *pemFile;
fopen_s(&pemFile, filename.c_str(), "r");
rsaPkey         = PEM_read_RSA_PUBKEY(pemFile, &rsaPkey, NULL, NULL);
fclose(pemFile);

if(rsaPkey == NULL)
    throw "Error pubkey file";

int size = RSA_size(rsaPkey);
unsigned char *encrypted;
encrypted = new unsigned char[size];

string instr  = "test";
int length = instr.length();
unsigned char *in = (unsigned char *)(instr.c_str());

unsigned long errorTrack = ERR_get_error() ;

int retEnc = RSA_public_encrypt(length, in, (unsigned char *)encrypted, rsaPkey, RSA_NO_PADDING);
errorTrack = ERR_get_error() ;
char *errorChar = new char[120];
errorChar = ERR_error_string(errorTrack, errorChar);

ERR_error_string 给我 error:0406B07A:lib(4):func(107):reason(122)

我如何才能找到关于此的更多详细信息,我在哪里可以找到库 4 和函数 107?

当我尝试使用 openssl cli 和相同的 public 密钥文件进行加密时,加密工作正常。

ERR_error_string gives me error:0406B07A:lib(4):func(107):reason(122)

How can I find more details about this, where can i find the library 4 and function 107 ?

我发现从 OpenSSL 错误代码中了解更多信息的最简单方法是:

$ openssl errstr 0406B07A
error:0406B07A:rsa routines:RSA_padding_add_none:data too small for key size

char *errorChar = new char[120];
errorChar = ERR_error_string(errorTrack, errorChar);

此外,来自 ERR_error_string man page

ERR_error_string() generates a human-readable string representing the error code e, and places it at buf. buf must be at least 256 bytes long. If buf is NULL, the error string is placed in a static buffer. Note that this function is not thread-safe and does no checks on the size of the buffer; use ERR_error_string_n() instead.

由于您使用的是 C++,这样的事情可能更容易:

std::string errorMsg;
errorMsg.resize(256);

(void)ERR_error_string(errorTrack, &errorMsg[0]);

以上,您正在使用 std::string 来管理资源。要获得 non-const 指针,您需要获取第一个元素的地址。

如果需要,您可以通过以下方式适当调整 errorMsg 的大小:

(void)ERR_error_string(errorTrack, &errorMsg[0]);
errorMsg.resize(std::strlen(errorMsg.c_str()));

这是另一个可能使 C++ 更易于使用的技巧。

typedef unsigned char byte;
...

std::string encrypted;
int size = RSA_size(rsaPkey);

if (size < 0)
    throw std::runtime_error("RSA_size failed");

// Resize to the maximum size
encrypted.resize(size);
...

int retEnc = RSA_public_encrypt(length, in, (byte*)&encrypted[0], rsaPkey, RSA_NO_PADDING);

if (retEnc < 0)
    throw std::runtime_error("RSA_public_encrypt failed");

// Resize the final string now that the size is known
encrypted.resize(retEnc );

以上,您正在使用 std::string 来管理资源。要获得 non-const 指针,您需要获取第一个元素的地址。

此外,NO_PADDING 通常不是一个好主意。您通常需要 OAEP 填充。请参阅 RSA_public_encrypt man page 中关于填充如何影响最大大小的注释。


C++ 可以使 OpenSSL 的使用更容易。您可以使用 unique_ptr 避免显式调用 EVP_CIPHER_CTX_free 等函数。见EVP Symmetric Encryption and Decryption | C++ Programs on the OpenSSL wiki, ,

在您的情况下,管理资源看起来像

using FILE_ptr = std::unique_ptr<FILE, decltype(&::fclose)>;
using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;