0

使用 aes 加密对数据进行加密时出现问题。这是源代码:

    std::string aes_encrypt( std::string text, std::string password ){
    EVP_CIPHER_CTX ectx;
    std::string key = sha256(password);
    std::string iv = sha256("aes_iv_"+password);
    int size = text.size();
    unsigned char* out = (unsigned char*)malloc( size );
    int outlen = 0;
    int tlen = 0;

    EVP_CIPHER_CTX_init( &ectx );
    EVP_EncryptInit( &ectx, EVP_aes_256_cbc(), (const unsigned char*)key.c_str(), (const unsigned char*)iv.c_str() );
    EVP_EncryptUpdate( &ectx, out, &outlen, (const unsigned char*)text.c_str(), text.size() );
    tlen += outlen;
    EVP_EncryptFinal( &ectx, out+tlen, &outlen );
    tlen += outlen;
    EVP_CIPHER_CTX_cleanup( &ectx );

    std::string data( (const char*)out, tlen );
    free( out );
    return data;
}

我的应用程序将在这一行崩溃:free(out); 有什么办法可以解决这个问题吗?

4

1 回答 1

1

在“out = (unsigned char*) malloc(size)”行中,您假设输出永远不会长于输入。情况并非如此(openssl 文档说您必须添加诸如块大小之类的东西),因此我相信您在加密期间 malloc 的缓冲区中有缓冲区溢出,这会导致 free(.. .) call --- 当你通过溢出一个 malloc'ated 缓冲区来破坏 malloc/free 数据结构时,这是很常见的事情。

于 2013-05-15T09:29:38.843 回答