使用OpenSSL rsa密钥解密时出错

时间:2014-05-07 14:04:17

标签: encryption openssl rsa public-key-encryption

我正在尝试使用OpenSSL RSA加密编写一个简单的应用程序来加密/解密unsigned char的缓冲区。我有我的公钥并使用

加密数组
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_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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

我对最后的零点有点怀疑,但它可能没问题。但是,当我尝试用

解密它时
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

我无法理解。有人知道这意味着什么以及它产生的原因吗?

1 个答案:

答案 0 :(得分:2)

Manual page表示:RSA_public_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);