我有一个问题,即使用EVP_SealInit,EVP_SealUpdate和EVP_SealFinal的加密过程正在运行,并且没有返回任何失败代码 .

尝试解密消息通过EVP_OpenInit和EVP_OpenUpdate工作,但是EVP_OpenFinal失败返回0.尽管函数返回0,但完全解密的文本存储在输出缓冲区中,并且从EVP_OpenFinal返回的总输出长度与从返回的总长度相同 . EVP_SealFinal .

我引用了这篇SO帖子:OpenSSL RSA: Unable to encrypt/decrypt messages longer than 16 bytes

我相信OP修改了他的代码修复,所以我无法在那里得到很多帮助 . 值得注意的是,无论消息长度如何,我的问题都存在 . 在EVP_OpenFinal调用期间,10,15和140个char消息都失败,但每个消息都完全存储在输出缓冲区中 .

加密:

int envelope_seal(EVP_PKEY *pub_key, uint8_t const *plaintext, 
                      int plaintext_len, uint8_t *encrypted_key, 
                      int *encrypted_key_len, uint8_t *iv,
                      uint8_t *ciphertext)
    {
        EVP_CIPHER_CTX *ctx = NULL;

        int ciphertext_len = 0;
        int len = 0;

        if (!(ctx = EVP_CIPHER_CTX_new()))
        {
            handleErrors();

            return -1;
        }

        if (!EVP_SealInit(ctx, EVP_aes_256_gcm(), &encrypted_key,
                  encrypted_key_len, iv, &pub_key, 1))
        {
            handleErrors();

            return -1;
        }


        if (!EVP_SealUpdate(ctx, ciphertext + len, &len, plaintext, plaintext_len))
        {
            handleErrors();

            return -1;
        }

        ciphertext_len += len;

        if (!EVP_SealFinal(ctx, ciphertext + ciphertext_len, &len))
        {
            handleErrors();

            return -1;
        }

        ciphertext_len += len;

        EVP_CIPHER_CTX_free(ctx);

        return ciphertext_len;
    }

解密:

int envelope_open(EVP_PKEY *priv_key, uint8_t const *ciphertext, 
                     int ciphertext_len, uint8_t const *encrypted_key, int encrypted_key_len, uint8_t const *iv, uint8_t *plaintext)
    {
        EVP_CIPHER_CTX *ctx = NULL;

        int plaintext_len = 0;
        int len = 0;

        if (!(ctx = EVP_CIPHER_CTX_new()))
        {
            handleErrors();

            return -1;
        }

        if (!EVP_OpenInit(ctx, EVP_aes_256_gcm(), encrypted_key,
                  encrypted_key_len, iv, priv_key))
        {
            handleErrors();

            return -1;
        }

        if (!EVP_OpenUpdate(ctx, plaintext + plaintext_len, &len, ciphertext, ciphertext_len))
        {
            handleErrors();

            return -1;
        }

        plaintext_len += len;

        if (!EVP_OpenFinal(ctx, plaintext + plaintext_len, &len))
        {
            handleErrors();

            return -1;

        }

        plaintext_len += len;

        EVP_CIPHER_CTX_free(ctx);

        return plaintext_len;
    }

错误处理:

void handleErrors(void)
    {
        ERR_print_errors_fp(stderr);
    }

如果任何人都可以强调OpenUpdate和OpenFinal之间的内部差异,那么任何帮助突出我可能会忽略的内容都会很棒 .

谢谢!