OpenSSL错误0200B009错误的文件描述符

OpenSSL错误0200B009错误的文件描述符,openssl,encryption,demo,Openssl,Encryption,Demo,运行OpenSSL CMS加密和解密演示时,我收到以下错误: Error Decrypting Data 2900476676:error:0200B009:system library:fread:Bad file descriptor:bss_file.c:245: 2900476676:error:20082002:BIO routines:FILE_READ:system lib:bss_file.c:246: 2900476676:error:0606506D:digital enve

运行OpenSSL CMS加密和解密演示时,我收到以下错误:

Error Decrypting Data
2900476676:error:0200B009:system library:fread:Bad file descriptor:bss_file.c:245:
2900476676:error:20082002:BIO routines:FILE_READ:system lib:bss_file.c:246:
2900476676:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:460:
它发生在
CMS\u decrypt()
方法中

有人知道怎么了吗

更新#1:

我正在使用objective-c中的库(也在c++中尝试过)。 它发生在本节中:

    int error = CMS_decrypt(cms, rkey, rcert, /*out*/ bout, NULL, 0);
    if (!error) {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
        printf("error code: %d\n", ERR_get_error());
        assert(false);
    }
更新#2:

添加了完全解密源。

- (void) decryptOrig {
    BIO *in = NULL, *out = NULL, *tbio = NULL;
    X509 *rcert = NULL;
    EVP_PKEY *rkey = NULL;
    CMS_ContentInfo *cms = NULL;
    int ret = 1;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *iosPathToFile = [NSString stringWithFormat:@"%@/encrypted.enc", documentsDirectory]; //[[NSBundle mainBundle] pathForResource:@"encrypted" ofType:@"enc"];
    NSString *iosPathToCertificate = [[NSBundle mainBundle] pathForResource:@"signer" ofType:@"pem"];
    NSString *iosPathToKey = [[NSBundle mainBundle] pathForResource:@"christof" ofType:@"key"];


    NSString *iosPathToOrigFinal = [NSString stringWithFormat:@"%@/original.txt", documentsDirectory];




    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    /* Read in recipient certificate and private key */
    tbio = BIO_new_file([iosPathToCertificate cStringUsingEncoding:NSUTF8StringEncoding], "r");



    if (!tbio)
        goto err;

    rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);

    /*BIO *output = BIO_new(BIO_s_mem());
    X509_print(output, rcert);
    char *temp = malloc(50000);
    BIO_read(output, temp, 50000);

    printf("cert: %s", temp);*/

    //temp for output
    BIO *bout = BIO_new_fp (stdout, BIO_NOCLOSE);


    BIO_reset(tbio);

    rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);

    //EVP_PKEY_print_private(bout, rkey, 0, NULL);

    if (!rcert || !rkey)
        goto err;

    /* Open S/MIME message to decrypt */

    in = BIO_new_file([iosPathToFile cStringUsingEncoding:NSUTF8StringEncoding], "r");



    if (!in)
        goto err;

    /* Parse message */
    cms = SMIME_read_CMS(in, NULL);

    //CMS_ContentInfo_print_ctx(bout, cms, 0, NULL);


    if (!cms)
        goto err;

    out = BIO_new_file([iosPathToOrigFinal cStringUsingEncoding:NSUTF8StringEncoding], "w");
    NSLog(iosPathToOrigFinal);
    /*char *mytestoutput = malloc(50000);
    memset(mytestoutput, 0, 50000);
    out = BIO_new_mem_buf(mytestoutput, 50000);*/

    if (!out)
        assert(false);

    /* Decrypt S/MIME message */
    int error = CMS_decrypt(cms, rkey, rcert, out, NULL, 0);
    if (!error) {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
        printf("error code: %d\n", ERR_get_error());
        assert(false);
    }


    ret = 0;

err:

    if (ret)
    {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
    }

    if (cms)
        CMS_ContentInfo_free(cms);
    if (rcert)
        X509_free(rcert);
    if (rkey)
        EVP_PKEY_free(rkey);

    if (in)
        BIO_free(in);
    if (out)
        BIO_free(out);
    if (tbio)
        BIO_free(tbio);

    return ret;

}
我已经删除了
bout
并在加密方法中使用了
out

更新#3:


对称加密类型是否可能存在问题?CBC等?

部分问题可能是您为解密而进行的方法调用有点麻烦-这是一个棘手的问题

您将您的输出文件放在它希望找到输入内容文件的位置-这也会留下一个null bio,它希望在其中放置纯文本,因此它会回退此错误

我想应该是这样的

int error = CMS_decrypt(cms, rkey, rcert, NULL, out, 0);
不是

我认为发生的事情实际上是方法签名本身在库中的某个点上发生了变化,但是这个旧的方法签名仍然存在于cms功能的一些旧的演示代码中。我想他们是为了更好地适应他们的习惯而改变的


<好运>

你用什么命令行?”萨尔诺德:哦,对不起,我忘了提到我在C++中使用了这个库(ObjuleC)。我要更新这个。顺便问一下,你能添加初始化参数的代码吗?我惊讶地发现,成功时返回
1
,失败时返回
0
<代码>如果(!error)正确但笨拙。我想如果将该变量命名为
success
,您会更高兴。@sarnold:我已经更正了输入错误并添加了源代码。该错误必须出现在解密中,但当我使用终端加密文件/msg时,会发生与我使用自我实现的加密方法时完全相同的错误(如上所述)。我想知道是否
BIO*bout=BIO_new_fp(stdout,BIO_NOCLOSE)成功还是失败?iOS应用程序上的标准输出是什么?
int error = CMS_decrypt(cms, rkey, rcert, out, NULL, 0); // won't decrypt