简体   繁体   English

使用 Botan 时出现 Seg 错误

[英]Seg fault when using Botan

I'm just getting started with Botan.我刚开始接触Botan。 I have included the botan_all.h in my code file and am linking to the libbotan-2.a library when building.我已将botan_all.h包含在我的代码文件中,并在构建时链接到libbotan-2.a库。

Here is the relevant part of main.cpp:这是 main.cpp 的相关部分:

#include "botan_all.h"

int main(int argc, char *argv[])
{
    const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
    std::unique_ptr<Botan::Cipher_Mode> enc = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::ENCRYPTION);    
    enc->set_key(key);
}

The enc->set_key(key) causes a seg fault. enc->set_key(key)会导致段错误。 What am I missing?我错过了什么?

This is likely caused by your Botan version not being build with support for the specified algorithm.这可能是由于您的 Botan 版本未在支持指定算法的情况下构建。 The method CipherMode::create returns a nullptr if the specified algorithm is not found ( reference ).如果未找到指定的算法( 参考),则CipherMode::create方法返回一个nullptr You would then have to check the result, eg:然后您必须检查结果,例如:

    if (enc == nullptr) {
      throw std::runtime_error{"Cipher not supported!"};
    }

Alternatively, you could use CipherMode::create_or_throw ( reference ):或者,您可以使用CipherMode::create_or_throw参考):

Botan::Cipher_Mode::create_or_throw("AES-128/CBC/PKCS7", Botan::ENCRYPTION);

You can also check your Botan headers for support for these specific algorithms, by checking if BOTAN_HAS_AES and BOTAN_HAS_MODE_CBC are defined.您还可以通过检查BOTAN_HAS_AESBOTAN_HAS_MODE_CBC是否已定义来检查您的 Botan 标头是否支持这些特定算法。 If these are not available, you would have to ensure that when you compile botan, you include AES and CBC support, eg with:如果这些不可用,则必须确保在编译 botan 时包含 AES 和 CBC 支持,例如:

./configure.py --enable-modules=aes,cbc,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM