使用openssl开源AES算法,实现aes、aes-cbc和aes-ecb对字符串的加解密

本文介绍了一个AES加密解密的实用工具库实现,包括CBC、ECB模式下的加密和解密功能,并通过MD5生成固定长度的密钥。此外,还提供了一个简单的测试案例来验证这些功能的有效性。

注意事项

  • 对于用户输入的密码进行了md5运算,从而保证数据格式的统一性

  • 内部调用的随机函数,参考我的其他博文 参考链接

头文件crypto_util.h

#pragma once

#include <string>

namespace hsm{
    namespace mgmt{

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

        std::string aes_encrypt_to_string(const std::string &string,
                                 const std::string &password);

        std::string aes_decrypt_from_string(const std::string &string,
                                          const std::string &password);

        std::string aes_ecb_encrypt_to_string(const std::string &string,
                                          const std::string &password);

        std::string aes_ecb_decrypt_from_string(const std::string &string,
                                            const std::string &password);

        std::string aes_cbc_encrypt_to_string(const std::string &string,
                                          const std::string &password);

        std::string aes_cbc_decrypt_from_string(const std::string &string,
                                            const std::string &password);
    }//namespace mgmt
}//namespace hsm

源文件crypto_util.cpp

#include "../util/crypto_util.h"

#include <cstring>
#include <memory>

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

namespace hsm{
    namespace mgmt{

        void get_md5_digest(const std::string &data,uint8_t result[16]){
            MD5_CTX md5_ctx{};
            MD5_Init(&md5_ctx);
            MD5_Update(&md5_ctx,data.c_str(),data.length());
            MD5_Final(result,&md5_c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值