C++编程:简单的AES加解密示例程序

0. 引言

AES 密钥是高级加密标准(AES)的核心部分,用于加密和解密数据。密钥的长度通常为128位、192位或256位,对应不同的安全级别。AES 使用对称加密技术,这意味着加密和解密使用的是同一个密钥。

以下是一个简单的 C++ 示例程序,演示如何使用 AES 进行加密和解密(利用 OpenSSL 库):

1. 示例代码

#include <openssl/evp.h>
#include <openssl/rand.h>

#include <cstdint>
#include <cstring>
#include <iostream>

// Handles errors by printing a message and exiting the program.
void HandleErrors() {
  std::cerr << "An error occurred!" << std::endl;
  exit(EXIT_FAILURE);
}

// Encrypts plaintext using AES-256-CBC and writes the ciphertext.
void AesEncrypt(const uint8_t* plaintext, int32_t plaintext_len, const uint8_t* key, const uint8_t* iv,
                uint8_t* ciphertext, int32_t* ciphertext_len) {
  EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  if (!ctx) HandleErrors();

  if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv) != 1) {
    HandleErrors();
  }

  int32_t len;
  if (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len) != 1) {
    HandleErrors();
  }
  *ciphertext_len = len;

  if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) {
    HandleErrors();
  }
  *ciphertext_len += len;

  EVP_CIPHER_CTX_free(ctx);
}

// Decrypts ciphertext using AES-256-CBC and writes the plaintext.
void AesDecrypt(const uint8_t* ciphertext, int32_t ciphertext_len, const uint8_t* key, const uint8_t* iv,
                uint8_t* plaintext, int32_t* plaintext_len) {
  EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  if (!ctx) HandleErrors();

  if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv) != 1) {
    HandleErrors();
  }

  int32_t len;
  if (EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len) != 1) {
    HandleErrors();
  }
  *plaintext_len = len;

  if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) {
    HandleErrors();
  }
  *plaintext_len += len;

  EVP_CIPHER_CTX_free(ctx);
}

int32_t main() {
  const uint8_t* plaintext = reinterpret_cast<const uint8_t*>("Hello, AES Encryption!");
  uint8_t key[32];  // 256-bit key
  uint8_t iv[16];   // 128-bit IV

  // Generate random key and IV
  if (!RAND_bytes(key, sizeof(key)) || !RAND_bytes(iv, sizeof(iv))) {
    std::cerr << "Error generating random key or IV!" << std::endl;
    return EXIT_FAILURE;
  }

  uint8_t ciphertext[128];
  int32_t ciphertext_len;

  AesEncrypt(plaintext, std::strlen(reinterpret_cast<const char*>(plaintext)), key, iv, ciphertext, &ciphertext_len);

  std::cout << "Ciphertext (hex): ";
  for (int32_t i = 0; i < ciphertext_len; ++i) {
    std::cout << std::hex << static_cast<int32_t>(ciphertext[i]);
  }
  std::cout << std::endl;

  uint8_t decryptedtext[128];
  int32_t decryptedtext_len;

  AesDecrypt(ciphertext, ciphertext_len, key, iv, decryptedtext, &decryptedtext_len);

  // Null-terminate the decrypted text
  decryptedtext[decryptedtext_len] = '\0';

  std::cout << "Decrypted text: " << reinterpret_cast<const char*>(decryptedtext) << std::endl;

  return 0;
}

2. 编译和运行

+. 确保你的系统已安装 OpenSSL 库。

  • 在 Linux 上,你可以使用 sudo apt install libssl-dev 安装。

+. 使用以下命令编译程序:

g++ -o aes_example aes_example.cpp -lcrypto -lssl

+. 运行程序:

./aes_example

程序输出

$ ./aes_example                                                                                                                                                                                 
Ciphertext (hex): c65ebb17df4046bee6c3e9d532676ecd23cff873c115fd3f9c15dc88a4a299c
Decrypted text: Hello, AES Encryption!

此示例生成随机密钥和 IV,演示了如何加密和解密数据。你可以根据需求调整代码以使用固定密钥和 IV(例如,从文件中读取)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘色的喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值