问题OpenSSL库 [英] Problem with OpenSSL library
问题描述
我尝试使用RSA算法简单的文本进行加密。我有一个问题,我的code。
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 );
RSA_public_encrypt的返回值为128
RSA_private_decrypt返回-1。如果我尝试用COUT显示decrypttext我什么也得不到。
感谢。
The return value of RSA_public_encrypt is 128.
RSA_private_decrypt return -1. If I try to display decrypttext with cout i get nothing.
Thanks.
推荐答案
您不应传递1 RSA_private_decrypt
,但您要解密块,即长度。 RSA_public_encrypt
= 128的返回值,你不知道明文的长度时,您解密!
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'
来源:
#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;
}
这篇关于问题OpenSSL库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!