AES128加密C++代码
时间: 2023-09-04 10:13:04 浏览: 175
以下是使用 Crypto++ 库实现 AES128 加密的 C++ 代码:
```c++
#include <iostream>
#include <string>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
using namespace CryptoPP;
std::string aes128_encrypt(const std::string& key, const std::string& iv, const std::string& plaintext)
{
std::string ciphertext;
CBC_Mode<AES>::Encryption encryption((byte*)key.c_str(), key.size(), (byte*)iv.c_str());
StringSource(plaintext, true, new StreamTransformationFilter(encryption, new StringSink(ciphertext)));
return ciphertext;
}
int main()
{
std::string key = "0123456789abcdef";
std::string iv = "fedcba9876543210";
std::string plaintext = "Hello, world!";
std::string ciphertext = aes128_encrypt(key, iv, plaintext);
std::cout << "Plaintext: " << plaintext << std::endl;
std::cout << "Ciphertext: " << ciphertext << std::endl;
return 0;
}
```
注意,在使用 Crypto++ 前,需要先安装 Crypto++ 库。可以通过以下命令在 Ubuntu 下进行安装:
```sh
sudo apt-get update
sudo apt-get install -y libcrypto++-dev
```
阅读全文