- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 RSA_public_encrypt
加密数据,但它似乎不起作用(retEnc
始终为 -1)。我还尝试使用 ERR_get_error
和 ERR_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 和相同的公钥文件进行加密时,加密工作正常。
最佳答案
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
来管理资源。要获取非常量指针,您需要获取第一个元素的地址。
如果需要,您可以使用以下方法适当调整 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
来管理资源。要获取非常量指针,您需要获取第一个元素的地址。
此外,NO_PADDING
通常不是一个好主意。您通常需要 OAEP 填充。请参阅 RSA_public_encrypt
man page 中的注释关于填充如何影响最大尺寸。
C++ 可以使 OpenSSL 的使用更加容易。您可以使用 unique_ptr
避免显式调用 EVP_CIPHER_CTX_free
等函数。参见 EVP Symmetric Encryption and Decryption | C++ Programs在 OpenSSL wiki 上,unique_ptr and OpenSSL's STACK_OF(X509)* , How to get PKCS7_sign result into a char * or std::string等
在你的例子中,它看起来像 these would be helpful管理资源:
using FILE_ptr = std::unique_ptr<FILE, decltype(&::fclose)>;
using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
关于c++ - rsa_public_encrypt 返回-1,错误0x0406B07A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069286/
下面是我更新的代码。这适用于加密但无法解密。我不想要填充,因此我不使用填充,这也确保每次运行的加密输出都相同。 #include #include #include #include #inc
我正在尝试使用 OpenSSL 在 C 中编写一个简单的加密例程,但我发现了一些奇怪的东西。我不是 C 大师,也不是 OpenSSL 专家。所以我可能犯了一个错误。 函数如下 char *rsa_en
我正在尝试使用 RSA_public_encrypt 加密数据,但它似乎不起作用(retEnc 始终为 -1)。我还尝试使用 ERR_get_error 和 ERR_error_string 查找有关
我正在尝试使用 RSA_public_encrypt 加密十进制数字 1,不带填充。当然这次加密的结果应该又是1。但实际上是 密码: 522867335089565389638320105881546
RSA_size(rsa) 在我的应用程序中返回等于 256 的模数。我正在使用 RSA_PKCS1_OAEP_PADDING 选项加密数据,因此发送到 RSA_public_encrypt() 的输
我一直在尝试使用 openssl 的 RSA_public_encrypt 加密测试字符串,但在我尝试运行它的每一种方式中都会导致段错误。 我尝试使用 BIO 检查我从 pem 文件中读取的 RSA
我是一名优秀的程序员,十分优秀!