c++ - RSA_private_decrypt错误

标签 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;
            outlen = RSA_public_encrypt(inlen, ptext, ctext, pubKey, 
    RSA_PKCS1_PADDING);
            if (outlen != RSA_size(pubKey)) exit(-1);
            _write(out, ctext, outlen);
        }


解密:

while (1) {     
        inlen = _read(in, ctext, key_size);
        printf("Read %i bytes\n", inlen);
        if (inlen <= 0) break;
        outlen = RSA_private_decrypt(key_size-11, ctext, ptext, privKey, RSA_PKCS1_PADDING);
        printf("RSA returns %i\n", outlen);
        if (outlen < 0)
        {
            fprintf(stderr, "OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
            exit(0);
        }
        _write(out, ptext, outlen);
}


程序输出:

Read 431 bytes
RSA returns -1
OpenSSL error: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error


key_size或key_size-11->不变。
帮助plz绕过此错误。

最佳答案

对于RSA解密,flen必须是RSA_size(pubkey),而不是RSA_size(pubkey) - 11。因此类似

outlen = RSA_private_decrypt(RSA_size(pubKey), ctext, ptext, privKey, RSA_PKCS1_PADDING);


应该管用。解密后的明文应包含在ctext[0] ... ctext[outlen-1]中。

关于c++ - RSA_private_decrypt错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46294070/

相关文章:

c++ - 友商+: no matching function for call

python - 删除水平下划线

c - 新手,无法使用 scanf() 解析输入,包括简单代码

c++ - 为每个源文件单独设置跟踪级别的技术

c - 对文本文件中的整数进行计数和求和时遇到问题

openssl - 微软压缩天然气 |如何将 PEM 编码的 ECDSA 私钥导入 MS Key Storage Provider

python - 使用 pyCrypto AES 解密验证 key 是否正确

c++ - 在不使用循环的情况下,将 ISBN-10 的前 9 位数字作为字符串读取并计算第 10 位数字

c++ - sizeof() 函数后跟方括号

c - 解决密码学问题,代码需要很长时间才能运行,需要对其进行优化