【发布时间】:2017-06-05 09:46:26
【问题描述】:
我是 libcrypto 库的初学者。我正在尝试将加密字符串提供给函数,对其进行解密并将其解密后返回。该字符串是用我的 4096 位大小的公钥编码的。
char* decodeStr(const char* str, const size_t sizeStr)
{
puts("Starting");
FILE* file = fopen(PRIVATE_KEY_PATH, "r");
if (file == NULL)
{
perror("Error while trying to access to Presto's private key.\n");
return NULL;
}
RSA *privateKey = RSA_new();
privateKey = PEM_read_RSAPrivateKey(file, &privateKey, NULL, NULL);
if (privateKey == NULL)
{
fprintf(stderr, "Error loading RSA private key.\n");
ERR_print_errors_fp(stderr);
return NULL;
}
char* res = malloc(sizeStr);
if (res == NULL)
{
perror("Memory allocating error ");
return NULL;
}
const int sizeDecoded = RSA_private_decrypt(sizeStr, str, res, privateKey, RSA_PKCS1_PADDING);
if (sizeDecoded == -1)
{
fprintf(stderr, "Error while decoding RSA-encoded wrapping key.\n");
ERR_print_errors_fp(stderr);
return NULL;
}
if ((res = realloc(res, (size_t)sizeDecoded)) == NULL)
{
perror("Memory allocating error ");
return NULL;
}
return res;
}
以下代码输出:
Starting
Error while decoding RSA-encoded wrapping key.
6928:error;04069506C:lib<4>:func<101>:reason<108>:.\crypto\rsa\rsa_eay.c:518:
由于错误未知,我无法在网上找到任何有关它的信息,而且我是 libcrypto 的初学者,str是否需要采用某种格式?
显然是这个破坏了程序,但我不能确定,我也不知道如何解决这个问题。
const int sizeDecoded = RSA_private_decrypt(sizeStr, str, res, privateKey, RSA_PKCS1_PADDING);
编辑:我一直在与一个客户合作,该客户为我提供了那些编码数据供我解密它们。我不知道它们是如何精确处理的。不幸的是,编码的字符串比私钥本身更敏感,所以我不能分享它。它看起来像 0c79cc00deb89a614db6ebe42be748219089fb5356,但有 1024 个字符。
【问题讨论】:
-
你能分享你的密文吗?还有你用来编码明文的代码/命令。
-
我一直在与一个客户合作,该客户为我提供了那些编码数据供我解密它们。我不知道他们是如何处理的。不幸的是,编码字符串比私钥本身更敏感。它看起来像
0c79cc00deb89a614db6ebe42be784821908e9fb5356,但包含数千个字符。 -
你可以下载openssl源代码并查看rsa_eay.c的第518行,它会让你更好地了解错误
-
比“千”更具体
-
相关,你应该避免
RSA_public_encrypt和RSA_private_decrypt。你当然应该避免RSA_PKCS1_PADDING。代替RSA_*_{en|de}crypt,使用EVP Asymmetric Encryption and Decryption。并使用 OAEP 填充而不是 PKCS 填充。另请参阅格林博士的A bad couple of years for the cryptographic token industry。
标签: c openssl rsa pem libcrypto