注意事项

  • 对于用户输入的密码进行了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_ctx);}
/*** @brief generate a valid aes key from input password** @note AES only support keys with length 128/192/256bits* @note this implementation use md5 as a method to fix the password*/std::unique_ptr<AES_KEY> get_aes_key(const std::string &password,int flag){auto aes_key = std::make_unique<AES_KEY>();uint8_t data[16]{};get_md5_digest(password,data);if (flag == AES_ENCRYPT){AES_set_encrypt_key(data,sizeof(data)*8,aes_key.get());} else if (flag == AES_DECRYPT){AES_set_decrypt_key(data,sizeof(data)*8,aes_key.get());}return aes_key;}std::string aes_encrypt_to_string(const std::string &data,const std::string &password){auto aes_key = get_aes_key(password,AES_ENCRYPT);std::string result(data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//encrypt blocksfor (size_t i = 0; i < data.length() / AES_BLOCK_SIZE ; ++i) {AES_encrypt(input_offset,output_offset,aes_key.get());input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//write rest od data to fileauto rest_input_length = data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}std::string aes_decrypt_from_string(const std::string &enc_data,const std::string &password){auto aes_key = get_aes_key(password,AES_DECRYPT);std::string result(enc_data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(enc_data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//decrypt blocksfor (size_t i = 0;i < enc_data.length() / AES_BLOCK_SIZE;i++){AES_decrypt(input_offset,output_offset,aes_key.get());input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//decrypt rest of dataauto rest_input_length = enc_data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}//aes-ecbstd::string aes_ecb_encrypt_to_string(const std::string &data,const std::string &password){auto aes_key = get_aes_key(password,AES_ENCRYPT);std::string result(data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//encrypt blocksfor (size_t i = 0; i < data.length() / AES_BLOCK_SIZE ; ++i) {AES_ecb_encrypt(input_offset,output_offset,aes_key.get(),AES_ENCRYPT);input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//write rest od data to fileauto rest_input_length = data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}std::string aes_ecb_decrypt_from_string(const std::string &enc_data,const std::string &password){auto aes_key = get_aes_key(password,AES_DECRYPT);std::string result(enc_data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(enc_data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//decrypt blocksfor (size_t i = 0;i < enc_data.length() / AES_BLOCK_SIZE;i++){AES_ecb_encrypt(input_offset,output_offset,aes_key.get(),AES_DECRYPT);input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//decrypt rest of dataauto rest_input_length = enc_data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}//aes-cbcstd::string aes_cbc_encrypt_to_string(const std::string &data,const std::string &password){unsigned char buffer[AES_BLOCK_SIZE] = {0};auto aes_key = get_aes_key(password,AES_ENCRYPT);std::string result(data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//encrypt blocksfor (size_t i = 0;i < 16;i++){buffer[i] += 1;}AES_cbc_encrypt(input_offset,output_offset,data.length(),aes_key.get(),buffer,AES_ENCRYPT);return result;}std::string aes_cbc_decrypt_from_string(const std::string &enc_data,const std::string &password){unsigned char buffer[AES_BLOCK_SIZE] = {0};auto aes_key = get_aes_key(password,AES_DECRYPT);std::string result(enc_data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(enc_data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);for (size_t i = 0;i < 16;i++){buffer[i] += 1;}AES_cbc_encrypt(input_offset,output_offset,enc_data.length(),aes_key.get(),buffer,AES_DECRYPT);return result;}}//namespace mgmt
}//namespace hsm

调用代码

#include <iostream>
#include <bitset>#include "util/crypto_util.h"
#include "include/random.h"int main() {std::string str;GenerateRandom(&str,32);std::cout << "GenerateRandom:" << str << "\n";//Test crypto_util md5
//    std::string str = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9";std::string password = "123456qweasdzxcv";//Test aes_cbcstd::string enc_string_cbc{};std::string dec_string_cbc{};enc_string_cbc = hsm::mgmt::aes_cbc_encrypt_to_string(str,password);std::cout << "enc_string_cbc enc:" <<enc_string_cbc << std::endl;dec_string_cbc = hsm::mgmt::aes_cbc_decrypt_from_string(enc_string_cbc,password);std::cout <<  "enc_string_cbc dec:" << dec_string_cbc << std::endl;std::cout << std::endl;//Test aes_ecbstd::string enc_string_ecb{};std::string dec_string_ecb{};enc_string_ecb = hsm::mgmt::aes_ecb_encrypt_to_string(str,password);std::cout << "enc_string_ecb enc:" <<enc_string_ecb << std::endl;dec_string_ecb = hsm::mgmt::aes_ecb_decrypt_from_string(enc_string_ecb,password);std::cout <<  "enc_string_ecb dec:" << dec_string_ecb << std::endl;std::cout << std::endl;//Test aes_stringstd::string enc_string{};std::string dec_string{};enc_string = hsm::mgmt::aes_encrypt_to_string(str,password);std::cout << "enc_string_aes enc:" <<enc_string << std::endl;dec_string = hsm::mgmt::aes_decrypt_from_string(enc_string_ecb,password);std::cout <<  "enc_string_aes dec:" << dec_string << std::endl;std::cout << std::endl;
}

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

  1. aes算法cbc模式c语言,AES算法及它的CBC加密模式

    AES四種加密模式的區別: ECB(Electronic Code Book電子密碼本)模式 ECB模式是最早采用和最簡單的模式,它將加密的數據分成若干組,每組的大小跟加密密鑰長度相同,然后每組都用相 ...

  2. aes算法的C语言实现代码,AES加密算法c语言实现代码

    AES加密算法c语言实现代码 #include "stdio.h" #include "memory.h" #include "time.h" ...

  3. python base64编码_JS和Python实现AES算法

    1. AES原理 AES算法是典型的对称加密算法,AES原理可以学习这两篇文档: 漫画:什么是AES算法:https://www.toutiao.com/i6783550080784794124/ A ...

  4. [JAVA实战篇] AES加密的JAVA实现及AES算法讲解

    AES加密算法原理 1. S-P结构加密 AES加密算法是2001年由美国提出的互联网加密算法,从密码学的角度来讲,AES是典型的S-P结构加密.什么是S-P结构加密呢,手残博主画了一张图帮助大家理解 ...

  5. 常见的加解密算法【MD5, AES, RSA等】

    目录 1. 摘要算法 MD5的算法过程 SHA1算法 MD5与SHA1算法的比较 2. 对称加密 AES加密算法 DES加密算法 3. 非对称加密 RSA DSA 1. 摘要算法 常见的摘要算法主要有 ...

  6. 奇妙的安全旅行之AES算法

    hi,大家好,今天开始我们来介绍一下对称加密算法中的AES算法. AES简介 AES(英语:Advanced Encryption Standard,缩写:AES),即高级加密标准,在密码学中又称Ri ...

  7. 前后台加解密的使用--SHA256算法 RSA算法 AES算法

    SHA256算法 sha256与md5一样是散列算法,不是加密算法,不存在解密的问题,因此是不可逆的,可以通过key+password,对密码进行加密,在后台进行比对,安全性比md5高一点,加密后生成 ...

  8. AES加解密基本原理

    1. 概述 在网络通信中,经常会用到加解密技术,其中AES加解密算法是比较广泛的应用于大块数据的对称加解密算法,本文主要介绍AES算法的一些基本原理,假设您对加解密.秘钥等知识有一定的认识,目标是为了 ...

  9. AES方式加解密的简单介绍

    上面一篇文章介绍了使用DES方式进行加解密( DES方式加解密的简单介绍),我们说了DES由于使用8个字节(64bit)密钥进行加解密,所以安全性不够(当然这里的不够都是相对的),所以现在使用了密钥更 ...

最新文章

  1. TEE Internal core API介绍(globalplatform)
  2. 利用Cydia Substrate进行Android HOOK(2)
  3. mybatis入门常见错误
  4. Faster-rcnn详解
  5. .NET5实战千万并发,性能碾压各版本,云原生时代,.NET5为王!
  6. 发布ASP.NET程序至IIS7
  7. semi-global matching 算法总结
  8. 长期没有工作是什么感觉?
  9. 最近在整理和准备发布
  10. React的单向数据流与组件间的沟通
  11. WinForm实现只打开一个窗口的代码
  12. python求解重叠区域线段覆盖总长度
  13. matlab基本图形处理实验,MATLAB数字图像处理实验讲义(指导书)
  14. winform窗体——布局方式
  15. C语言程序设计知识必备pdf,C语言程序设计基础知识要点.pdf
  16. OFDM学习笔记(七)(多址接入技术)
  17. 前端获取北京时间_分享js获取标准北京时间的代码 JS如何获取北京时间JS
  18. 【IT情感】关于专业、理想和工作的认知
  19. 远程桌面连接服务器时,键盘不能正常打字
  20. QT——http协议(大华摄像头保活,根据Id获取大华摄像头播放地址rtsp流)

热门文章

  1. idea 创建java文件_idea创建java文件 格式不对
  2. qt 添加依赖库lib_在QT中添加LIB的方法
  3. python删除mysql数据库_python 删除mysql数据库
  4. 【转】ABP源码分析二十:ApplicationService
  5. C# 有什么惊艳到你的地方?
  6. [Sharepoint2007对象模型]第一回:服务器场(SPFarm)
  7. C++11 FAQ中文版:std::function 和 std::bind
  8. java 无法执行export 命令_模块中的export、import以及复合模式的使用方法
  9. 执行本地sql_实用!5个在线 SQL 数据库环境
  10. 【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)