evp_decryptupdate()返回0

发布于 2025-02-07 11:40:40 字数 1425 浏览 0 评论 0原文

由于某种原因,当我通过32作为最后一个参数时,我从evp_decryptupdate()函数中获得

0解密成功。

#include <openssl/evp.h>
#include <stdio.h>
#include <string.h>

void dump_head(unsigned char *buf, size_t len)
{
    unsigned end, i;
    for (end = len; end > 0; end--)
        if (buf[end-1] != 0)
            break;
    printf("buf = {");
    for (i = 0; i < end; i++)
        printf(" %02hhx,", buf[i]);
    printf(" }\n");
}

int main(void)
{
    unsigned char key[] = "0123456789abcdef";
    unsigned char iv[] = "1234567887654321";
    unsigned char indata[32] = "0123456789abcdeffedcba9876543210";
    unsigned char buf[4096];
    unsigned pos;
    int cipher_len;
    EVP_CIPHER_CTX *ctx;

    ctx = EVP_CIPHER_CTX_new();
    EVP_CIPHER_CTX_init(ctx);
    EVP_CIPHER_CTX_set_padding(ctx, 0);
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);  
    printf("decrypt init: %d\n", EVP_DecryptInit_ex(ctx, EVP_aes_256_wrap_pad(), NULL, key, iv));

    printf("decrypt update: %d\n", EVP_DecryptUpdate(ctx, (unsigned char *)buf, &cipher_len, indata, 32));
        
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));
        
    printf("Final!\n");
    memset(buf, 0, sizeof(buf));
    EVP_DecryptFinal_ex(ctx, buf, &cipher_len);
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));

    return 0;
}

在这两种情况下,我都将cipher_len as 0。是否缺少一些参数?

For some reason, I am getting 0 from EVP_DecryptUpdate() function when I am passing 32 as its last parameter but when I changed it to 64 it returns 1. The buffer size is 32.

According to the documentation, I should get a 1 if the decrypt is successful.

#include <openssl/evp.h>
#include <stdio.h>
#include <string.h>

void dump_head(unsigned char *buf, size_t len)
{
    unsigned end, i;
    for (end = len; end > 0; end--)
        if (buf[end-1] != 0)
            break;
    printf("buf = {");
    for (i = 0; i < end; i++)
        printf(" %02hhx,", buf[i]);
    printf(" }\n");
}

int main(void)
{
    unsigned char key[] = "0123456789abcdef";
    unsigned char iv[] = "1234567887654321";
    unsigned char indata[32] = "0123456789abcdeffedcba9876543210";
    unsigned char buf[4096];
    unsigned pos;
    int cipher_len;
    EVP_CIPHER_CTX *ctx;

    ctx = EVP_CIPHER_CTX_new();
    EVP_CIPHER_CTX_init(ctx);
    EVP_CIPHER_CTX_set_padding(ctx, 0);
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);  
    printf("decrypt init: %d\n", EVP_DecryptInit_ex(ctx, EVP_aes_256_wrap_pad(), NULL, key, iv));

    printf("decrypt update: %d\n", EVP_DecryptUpdate(ctx, (unsigned char *)buf, &cipher_len, indata, 32));
        
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));
        
    printf("Final!\n");
    memset(buf, 0, sizeof(buf));
    EVP_DecryptFinal_ex(ctx, buf, &cipher_len);
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));

    return 0;
}

In both cases, I am getting cipher_len as 0. Are there some parameters I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文