在C++中实现AES加密算法,可以使用现有的密码库如Crypto++,也可以手动实现AES算法。
使用Crypto++库实现AES加密算法的示例代码如下:
#include <iostream>#include <string>#include <cryptopp/aes.h>#include <cryptopp/modes.h>#include <cryptopp/filters.h>#include <cryptopp/hex.h>std::string encryptAES(const std::string& plainText, const std::string& key){ CryptoPP::AES::Encryption aesEncryption((byte*)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (byte*)key.c_str()); std::string cipherText; CryptoPP::StringSource(plainText, true, new CryptoPP::StreamTransformationFilter(cbcEncryption, new CryptoPP::StringSink(cipherText) ) ); return cipherText;}int main(){ std::string key = "0123456789abcdef"; std::string plainText = "Hello, world!"; std::string cipherText = encryptAES(plainText, key); std::cout << "Cipher text: " << cipherText << std::endl; return 0;}这段代码使用Crypto++库实现了AES加密算法,将明文加密后输出密文。在实际使用中,需要在编译时链接Crypto++库。另外也可以自行实现AES算法,但这需要更多的工作和知识。


