Ubuntu20.04运行CryptoPP标准AES_CTR示例出现段错误求解决
问题描述
以下是CryptoPP官网提供的标准AES-CTR示例代码,该代码在Win11系统可正常运行,但在Ubuntu20.04系统执行时,CTR_Mode< AES >::Encryption e;行出现段错误,需修复方案。
原示例代码
// 编译命令 // g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread // g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread #include <cryptopp/osrng.h> using CryptoPP::AutoSeededRandomPool; #include <iostream> using std::cout; using std::cerr; using std::endl; #include <string> using std::string; #include <cstdlib> using std::exit; #include <cryptopp/cryptlib.h> using CryptoPP::Exception; #include <cryptopp/hex.h> using CryptoPP::HexEncoder; using CryptoPP::HexDecoder; #include <cryptopp/filters.h> using CryptoPP::StringSink; using CryptoPP::StringSource; using CryptoPP::StreamTransformationFilter; #include <cryptopp/hex.h> using CryptoPP::AES; #include <cryptopp/ccm.h> using CryptoPP::CTR_Mode; #include "assert.h" int main(int argc, char* argv[]) { AutoSeededRandomPool prng; CryptoPP::byte key[AES::DEFAULT_KEYLENGTH]; prng.GenerateBlock(key, sizeof(key)); CryptoPP::byte iv[AES::BLOCKSIZE]; prng.GenerateBlock(iv, sizeof(iv)); string plain = "CTR Mode Test"; string cipher, encoded, recovered; /*********************************\ \*********************************/ // Pretty print key encoded.clear(); StringSource(key, sizeof(key), true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "key: " << encoded << endl; // Pretty print iv encoded.clear(); StringSource(iv, sizeof(iv), true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "iv: " << encoded << endl; /*********************************\ \*********************************/ try { cout << "plain text: " << plain << endl; CTR_Mode< AES >::Encryption e; cout << "after definition1" << endl; e.SetKeyWithIV(key, sizeof(key), iv); cout << "after definition2" << endl; // The StreamTransformationFilter adds padding // as required. ECB and CBC Mode must be padded // to the block size of the cipher. StringSource(plain, true, new StreamTransformationFilter(e, new StringSink(cipher) ) // StreamTransformationFilter ); // StringSource } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } /*********************************\ \*********************************/ // Pretty print encoded.clear(); StringSource(cipher, true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "cipher text: " << encoded << endl; /*********************************\ \*********************************/ try { CTR_Mode< AES >::Decryption d; d.SetKeyWithIV(key, sizeof(key), iv); // The StreamTransformationFilter removes // padding as required. StringSource s(cipher, true, new StreamTransformationFilter(d, new StringSink(recovered) ) // StreamTransformationFilter ); // StringSource cout << "recovered text: " << recovered << endl; } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } /*********************************\ \*********************************/ return 0; }
修复方案
Ubuntu20.04仓库默认提供的CryptoPP版本为5.6.2,该版本与官网示例依赖的新版本API存在差异,导致段错误,可通过以下两种方式修复:
方法1:修正头文件引用(适配旧版本)
旧版本CryptoPP中,CTR_Mode类定义在<cryptopp/modes.h>而非<cryptopp/ccm.h>,同时代码中重复包含了<cryptopp/hex.h>,需修正:
- 将
#include <cryptopp/ccm.h>替换为#include <cryptopp/modes.h> - 删除重复的
#include <cryptopp/hex.h>
完整修复后的代码
// 编译命令 // g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread // g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread #include <cryptopp/osrng.h> using CryptoPP::AutoSeededRandomPool; #include <iostream> using std::cout; using std::cerr; using std::endl; #include <string> using std::string; #include <cstdlib> using std::exit; #include <cryptopp/cryptlib.h> using CryptoPP::Exception; #include <cryptopp/hex.h> using CryptoPP::HexEncoder; using CryptoPP::HexDecoder; using CryptoPP::AES; #include <cryptopp/filters.h> using CryptoPP::StringSink; using CryptoPP::StringSource; using CryptoPP::StreamTransformationFilter; #include <cryptopp/modes.h> using CryptoPP::CTR_Mode; #include "assert.h" int main(int argc, char* argv[]) { AutoSeededRandomPool prng; CryptoPP::byte key[AES::DEFAULT_KEYLENGTH]; prng.GenerateBlock(key, sizeof(key)); CryptoPP::byte iv[AES::BLOCKSIZE]; prng.GenerateBlock(iv, sizeof(iv)); string plain = "CTR Mode Test"; string cipher, encoded, recovered; // Pretty print key encoded.clear(); StringSource(key, sizeof(key), true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "key: " << encoded << endl; // Pretty print iv encoded.clear(); StringSource(iv, sizeof(iv), true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "iv: " << encoded << endl; try { cout << "plain text: " << plain << endl; CTR_Mode< AES >::Encryption e; cout << "after definition1" << endl; e.SetKeyWithIV(key, sizeof(key), iv); cout << "after definition2" << endl; StringSource(plain, true, new StreamTransformationFilter(e, new StringSink(cipher) ) // StreamTransformationFilter ); // StringSource } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } // Pretty print cipher text encoded.clear(); StringSource(cipher, true, new HexEncoder( new StringSink(encoded) ) // HexEncoder ); // StringSource cout << "cipher text: " << encoded << endl; try { CTR_Mode< AES >::Decryption d; d.SetKeyWithIV(key, sizeof(key), iv); StringSource s(cipher, true, new StreamTransformationFilter(d, new StringSink(recovered) ) // StreamTransformationFilter ); // StringSource cout << "recovered text: " << recovered << endl; } catch(const CryptoPP::Exception& e) { cerr << e.what() << endl; exit(1); } return 0; }
方法2:升级CryptoPP到最新版本
从源码编译安装最新版CryptoPP,步骤如下:
- 下载并解压最新源码包:
wget https://www.cryptopp.com/cryptopp870.zip unzip cryptopp870.zip cd cryptopp870
- 编译安装:
make sudo make install sudo ldconfig
- 编译代码时链接新版本库:
g++ -g -O2 -DNDEBUG Driver.cpp -o Driver.exe -lcryptopp -lpthread
说明
两种方法均可解决段错误问题,方法1无需修改系统库,适合快速适配;方法2可获得CryptoPP的最新特性和安全更新。
内容的提问来源于stack exchange,提问作者Jeremy Smith
相关产品推荐
相关产品推荐

