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

猜你喜欢

转载自blog.csdn.net/stallion5632/article/details/144180779
今日推荐