C++使用openssl实现aes加解密,其中加密是string到文件,解密是文件到string,切合项目背景

本文介绍了如何利用C++结合OpenSSL库实现AES加解密操作。加密过程将字符串写入文件,解密则从文件读取并还原为字符串。同时,为增强密码安全性,采用了MD5对用户输入的密码进行处理,确保密码长度固定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

  • 使用md5对于用户输入的密码进行保护,也使得密码的长度固定
  • crypto_util.h
#pragma once

#include <string>

namespace hsm{
namespace mgmt{

void get_md5_digest(const std::string &data,uint8_t result[16]);

void aes_encrypt_to_file(const std::string &file_name,
                         const std::string &password,const std::string &data);

std::string aes_decrypt_from_file(const std::string &file_name,
                                  const std::string &password);

}//namespace mgmt
}//namespace hsm
  • crypto_util.c
#include "crypto_util.h"

#include <cstring>
#include <fstream>
#include <memory>
#include <sstream>

#include <openssl/aes.h>
#include <openssl/md5.h>

namespace hsm{
namespace mgmt{

void get_m
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值