RSA_private_decrypt 返回 -1

发布于 2024-07-11 15:58:32 字数 592 浏览 8 评论 0原文

我尝试用RSA算法加密简单的文本。 我的代码有问题。

RSA        *_RSA ;
unsigned char text[2560] = "A";
unsigned char sectext[2560];
unsigned char decrypttext[2560];
int i = 0;

_RSA = RSA_generate_key ( 1024, 65537, NULL, NULL );
i = RSA_public_encrypt ( 1, text,    sectext,     _RSA, RSA_PKCS1_OAEP_PADDING );
i = RSA_private_decrypt( 1, sectext, decrypttext, _RSA, RSA_PKCS1_OAEP_PADDING);
RSA_free ( _RSA );

RSA_public_encrypt的返回值为128,即密文的大小。 RSA_private_decrypt 返回 -1,这是一个错误。 如果我尝试显示恢复的文本,那么我什么也得不到。

为什么RSA_private_decrypt返回-1?

I try to encrypt simple text with RSA algorithm. I have a problem with my code.

RSA        *_RSA ;
unsigned char text[2560] = "A";
unsigned char sectext[2560];
unsigned char decrypttext[2560];
int i = 0;

_RSA = RSA_generate_key ( 1024, 65537, NULL, NULL );
i = RSA_public_encrypt ( 1, text,    sectext,     _RSA, RSA_PKCS1_OAEP_PADDING );
i = RSA_private_decrypt( 1, sectext, decrypttext, _RSA, RSA_PKCS1_OAEP_PADDING);
RSA_free ( _RSA );

The return value of RSA_public_encrypt is 128, which is the size of the ciphertext. RSA_private_decrypt returns -1, which is an error. If I try to display the recovered text then I get nothing.

Why is RSA_private_decrypt returning -1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2024-07-18 15:58:32

您不应该将 1 传递给 RSA_private_decrypt ,而应该传递您尝试解密的块的长度,即。 RSA_public_encrypt的返回值=128。解密时你不知道明文的长度!

这个完整的示例程序结果:

128 from encrypt. 
1 from decrypt: 'A'

来源:

#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    RSA *myRSA;
    unsigned char cleartext[2560] = "A";
    unsigned char encrypted[2560] = { 0 };
    unsigned char decrypted[2560] = { 0 };
    int resultEncrypt = 0;
    int resultDecrypt = 0;

    myRSA = RSA_generate_key ( 1024, 65537, NULL, NULL );
    resultEncrypt = RSA_public_encrypt ( 1 /* strlen(cleartext) */, cleartext, encrypted, myRSA, RSA_PKCS1_OAEP_PADDING );
    printf("%d from encrypt.\n", resultEncrypt);
    resultDecrypt = RSA_private_decrypt( 128 /* resultEncrypt */, encrypted, decrypted, myRSA, RSA_PKCS1_OAEP_PADDING);
    printf("%d from decrypt: '%s'\n", resultDecrypt, decrypted);
    RSA_free ( myRSA );

    return 0;
}

You should not pass 1 to RSA_private_decrypt but the length of the block you are trying to decrypt, ie. the return value of RSA_public_encrypt = 128. You do not know the length of the cleartext when you are decrypting!

This complete sample program results in:

128 from encrypt. 
1 from decrypt: 'A'

Source:

#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    RSA *myRSA;
    unsigned char cleartext[2560] = "A";
    unsigned char encrypted[2560] = { 0 };
    unsigned char decrypted[2560] = { 0 };
    int resultEncrypt = 0;
    int resultDecrypt = 0;

    myRSA = RSA_generate_key ( 1024, 65537, NULL, NULL );
    resultEncrypt = RSA_public_encrypt ( 1 /* strlen(cleartext) */, cleartext, encrypted, myRSA, RSA_PKCS1_OAEP_PADDING );
    printf("%d from encrypt.\n", resultEncrypt);
    resultDecrypt = RSA_private_decrypt( 128 /* resultEncrypt */, encrypted, decrypted, myRSA, RSA_PKCS1_OAEP_PADDING);
    printf("%d from decrypt: '%s'\n", resultDecrypt, decrypted);
    RSA_free ( myRSA );

    return 0;
}
悲凉≈ 2024-07-18 15:58:32

尝试调用 ERR_error_string() 获取文本错误的描述。

Try calling ERR_error_string() to get a text description of the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文