Encryption 使用OpenSSL rsa密钥解密时出错

Encryption 使用OpenSSL rsa密钥解密时出错,encryption,openssl,rsa,public-key-encryption,Encryption,Openssl,Rsa,Public Key Encryption,我正在尝试编写一个简单的应用程序,使用OpenSSL RSA加密对无符号字符的缓冲区进行加密/解密。我有我的公钥并用 unsigned char plain [13] = "Hello World!"; unsigned char encrypted[1024]={}; unsigned char decrypted[1024]={}; int padding = RSA_PKCS1_OAEP_PADDING; int flen = 13; int res = RSA_public_encryp

我正在尝试编写一个简单的应用程序,使用OpenSSL RSA加密对
无符号字符的缓冲区进行加密/解密。我有我的公钥并用

unsigned char plain [13] = "Hello World!";
unsigned char encrypted[1024]={};
unsigned char decrypted[1024]={};
int padding = RSA_PKCS1_OAEP_PADDING;
int flen = 13;
int res = RSA_public_encrypt(flen, plain, encrypted, rsa_pbk, padding);
其中,
rsa\u pbk
是包含密钥的rsa结构。如果我打印它,结果是以下十六进制数组:

13 d0 44 a3 2b 12 67 d8 e2 aa cf 53 6c 81 ed e9 9e 2d 9c dd 1d 28 84 5b 60 93 58 1c 7f eb b 66 26 39 8c 27 48 11 31 6 53 90 16 2e da 5c 7e 48 3e 15 c2 19 d3 10 79 71 1a fa f7 c1 57 93 82 f2 95 1 e d8 70 ba 1b 7e 12 d5 a 34 75 8f 2f 3c a6 60 f1 4b 60 6c 94 3e 4b 72 61 81 fb 89 e2 1e 5a 8 48 55 a5 5f 44 3b a4 e2 16 eb 7e 87 10 18 2e 1b 82 e7 86 43 69 21 ec a5 98 4 de 90 c5 5a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我对结尾的零有点怀疑,但可能没问题。但是,当我试图用

flen = keysize - 50;
RSA_private_decrypt(flen, encrypted, decrypted, this->rsa_pvk, padding);
我得到以下错误

error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
我无法理解。有人知道它的含义和产生的原因吗?

声明:RSA\u public\u encrypt()返回加密数据的大小。RSA_private_decrypt()返回恢复的明文的大小。出现错误时,返回-1;错误代码可以通过ERR_get_error(3)获得

您的代码应该如下所示:

unsigned char plain [13] = "Hello World!";
unsigned char encrypted[1024]={};
unsigned char decrypted[1024]={};
int padding = RSA_PKCS1_OAEP_PADDING;
int flen = 13;
int res = RSA_public_encrypt(flen, plain, encrypted, rsa_pbk, padding);
flen = res;
res = RSA_private_decrypt(flen, encrypted, decrypted, this->rsa_pvk, padding);