You need to enable JavaScript to run this app. C语言调用OpenSSL解密AES 192 ECB数据时出现Segmentation fault问题
优惠活动
大模型
产品
解决方案
定价
更多

C语言调用OpenSSL解密AES 192 ECB数据时出现Segmentation fault问题

AES 192 ECB解密触发段错误问题

我在尝试解密AES 192 ECB加密的消息时遇到段错误,目前无法定位问题根因。我清楚ECB模式安全性不足,本次使用场景不涉及生产环境。

  • 我已验证readFile方法可用:将读取的密文回写至文件后通过diff工具对比确认无异常
  • 密钥长度为24字节,已通过ls和hexdump工具核验
  • 据我所知ECB模式无需IV向量,因此我将IV参数设置为NULL
  • 解密逻辑参考了OpenSSL官方文档示例
  • 我推测问题出在EVP_DecryptUpdate调用环节
  • 尝试使用gdb定位问题,但我对gdb操作不熟悉,仅得到如下报错

0x00007ffff798089d in ?? () from /usr/lib64/libcrypto.so.1.1

问题代码

#include <stdio.h>
#include <stdlib.h>

#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/conf.h>
#include <openssl/sha.h>

int readFile(char* filename, unsigned char** data){
        FILE *fp;
        unsigned char* tmp;
        long fsize = 0;

        if ((fp = fopen(filename, "rb")) == NULL){
                perror(filename);
                exit(EXIT_FAILURE);
        }

        // get length by going to end of the file and checking the position.
        // Rewinding to start afterwards
        fseek(fp, 0, SEEK_END);
        fsize = ftell(fp);
        rewind(fp);

        if((tmp = malloc(fsize))){
                fread(tmp, fsize, 1, fp);
                fclose(fp);
        } else {
                fclose(fp);
                perror(filename);
                exit(EXIT_FAILURE);
        }
        *data = tmp;
        return fsize;
}

int decryptAES192ECB(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext){
    EVP_CIPHER_CTX *ctx;
    int len;
    int plaintext_len;

    /* Create and initialise the context */
    if(!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors();

    /*
     * Initialise the decryption operation. IMPORTANT - ensure you use a key
     * and IV size appropriate for your cipher
     */
    if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_192_ecb(), NULL, key, NULL))
        handleErrors();

    /*
     * Provide the message to be decrypted, and obtain the plaintext output.
     * EVP_DecryptUpdate can be called multiple times if necessary.
     */
    if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        handleErrors();
    plaintext_len = len;

    /*
     * Finalise the decryption. Further plaintext bytes may be written at
     * this stage.
     */
    if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
        handleErrors();
    plaintext_len += len;

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

int main(int argc, char **argv){
        unsigned char *source_cipher, *source_key;
        long source_cipher_len = readFile("source-cipher.bin", &source_cipher);
        long source_key_len = readFile("source-key.bin", &source_key);

        unsigned char* plaintext;
        decryptAES192ECB(source_cipher, source_cipher_len, source_key, plaintext);

        return 0;
}

解决方案

  1. 在main方法中调用decryptAES192ECB前添加内存分配代码:
unsigned char* plaintext = malloc(source_cipher_len);
  1. 删除decryptAES192ECB函数中EVP_DecryptFinal_ex调用后的handleErrors()语句

内容的提问来源于stack exchange,提问作者Akreter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 04:15:05