C++ RSA_private_解密错误
很抱歉这个愚蠢的问题,我看过很多例子,但找不到答案。 正在尝试使用rsa加密和解密文件: 加密C++ RSA_private_解密错误,c++,c,cryptography,rsa,C++,C,Cryptography,Rsa,很抱歉这个愚蠢的问题,我看过很多例子,但找不到答案。 正在尝试使用rsa加密和解密文件: 加密 ptext = (unsigned char *)malloc(key_size); ctext = (unsigned char *)malloc(key_size); while (1) { inlen = _read(in, ptext, key_size); if (inlen <= 0) break;
ptext = (unsigned char *)malloc(key_size);
ctext = (unsigned char *)malloc(key_size);
while (1) {
inlen = _read(in, ptext, key_size);
if (inlen <= 0) break;
outlen = RSA_public_encrypt(inlen, ptext, ctext, pubKey,
RSA_PKCS1_PADDING);
if (outlen != RSA_size(pubKey)) exit(-1);
_write(out, ctext, outlen);
}
按键大小或按键大小-11->无更改。
帮助plz绕过此错误。对于RSA解密,
flen
必须是RSA\u size(pubkey)
,而不是RSA\u size(pubkey)-11
。因此
outlen = RSA_private_decrypt(RSA_size(pubKey), ctext, ptext, privKey, RSA_PKCS1_PADDING);
应该有用。解密的明文应包含在
ctext[0]
中ctext[outlen-1]
这就是我在一个项目中使用RSA加密函数的方式。尝试与您的实现进行比较
int rsa_oaep_encrypt(EVP_PKEY *publicKey, const unsigned char *plaintext, const size_t plaintextLen, unsigned char *ciphertext, size_t *ciphertextLen)
{
int sts = -1;
if ( (*ciphertextLen = RSA_public_encrypt(plaintextLen, plaintext, ciphertext, publicKey->pkey.rsa, RSA_PKCS1_OAEP_PADDING)) > 0)
sts = 0;
return sts;
}
int rsa_oaep_decrypt(EVP_PKEY *privateKey, const unsigned char *ciphertext, const size_t ciphertextLen, unsigned char *plaintext, size_t *plaintextLen)
{
int sts = -1;
if ( (*plaintextLen = RSA_private_decrypt(ciphertextLen, ciphertext, plaintext, privateKey->pkey.rsa, RSA_PKCS1_OAEP_PADDING)) > 0)
sts = 0;
return sts;
}
您正在写入
RSA\u size(pubKey)
字节的数据,然后读取key\u size
字节。这是正确的吗?key\u size
是否等于RSA\u size(pubKey)
?是什么让你决定尝试从key\u size
中减去11?我将读取值更改为rsa\u size(pubKey),但得到了相同的错误。rsa\u size(pubKey)=512,key\u size=512直到相同的错误:读取512字节rsa返回-1 OpenSSL错误:错误:0407109F:rsa例程:rsa\u padding\u check\u PKCS1\u type\u 2:pkcs解码错误
outlen = RSA_private_decrypt(RSA_size(pubKey), ctext, ptext, privKey, RSA_PKCS1_PADDING);
int rsa_oaep_encrypt(EVP_PKEY *publicKey, const unsigned char *plaintext, const size_t plaintextLen, unsigned char *ciphertext, size_t *ciphertextLen)
{
int sts = -1;
if ( (*ciphertextLen = RSA_public_encrypt(plaintextLen, plaintext, ciphertext, publicKey->pkey.rsa, RSA_PKCS1_OAEP_PADDING)) > 0)
sts = 0;
return sts;
}
int rsa_oaep_decrypt(EVP_PKEY *privateKey, const unsigned char *ciphertext, const size_t ciphertextLen, unsigned char *plaintext, size_t *plaintextLen)
{
int sts = -1;
if ( (*plaintextLen = RSA_private_decrypt(ciphertextLen, ciphertext, plaintext, privateKey->pkey.rsa, RSA_PKCS1_OAEP_PADDING)) > 0)
sts = 0;
return sts;
}