头文件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);}//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;}}//namespace mgmt
}//namespace hsm

使用openssl,实现输入和输出都是字符串的类型,注意:输入最好是16的倍数相关推荐

  1. 使用openssl完成aes-cbc模式的数据加解密,输入和输出都是字符串的形式

    代码 #include <cstring> #include <memory>#include <openssl/aes.h> #include <opens ...

  2. 使用openssl完成aes-ecb模式的数据加解密,输入和输出都是字符串类型

    代码 #include <cstring> #include <memory>#include <openssl/aes.h> #include <opens ...

  3. 【2022年第一期 CANN训练营学习笔记】进阶班应用开发课 大作业1-开发DVPP应用,输入,输出都是JPEG图片,且分辨率不同

    1.大作业1题目如下:开发DVPP应用,输入,输出都是JPEG图片,且分辨率不同. 根据作业提示,转换的思路如下: 原始JPEG图片->JPEG解码->YUV Resize->JPE ...

  4. ACMNO.3 有三个整数a b c,由键盘输入,输出其中的最大的数。 输入 一行数组,分别为a b c 输出 a b c其中最大的数 样例输入 10 20 30 样例输出 30

    基于平台Dev-C++ 5.11 题目描述 有三个整数a b c,由键盘输入,输出其中的最大的数. 输入 一行数组,分别为a b c 输出 a b c其中最大的数 样例输入 10 20 30 样例输出 ...

  5. 编写一个C程序,实现以下功能:编写一个函数decTobin(int n),该函数能将一个十进制数n转换成二进制数,输入13 输出 1101。在main函数中输入整数n,调用函数,输出它的二进制

    题目要求: 编写一个C程序,实现以下功能: //编写一个函数decTobin(int n),该函数能将一个十进制数n转换成二进制数,输入13 输出 1101. //在main函数中输入整数n,调用函数 ...

  6. python输入和输出的区别_python2和python3的输入和输出区别介绍

    Python3 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdou ...

  7. python学习总结7 - 输入与输出【格式化字符串及读写文件】

    文章目录 输入与输出 1.更复杂的输出格式 1.1 格式化字符串字面值(3.6版本加入) 1.2 字符串的format方法 1.3 手动格式化字符串 1.4 旧式字符串格式化方法 2.读写文件 2.1 ...

  8. python输入年月日输出年月日_Python编程基础04:输入与输出

    一.IPO模型 一个程序,按照功能可以划分为三个部分:输入部分.处理部分和输出部分 本讲我们学习基本输入与输出,通过输入函数获取数据(数值型和字符串),通过输出函数将处理结果以某种方式呈现.处理部分会 ...

  9. python的格式化输入_一看就懂的Python输入和输出、格式化字符串方法

    程序的输出可以有多种形式:我们可以将数据以人类可读的形式打印到屏幕上,或者将其写入到文件中以供后续使用. 格式化输出 迄今为止,在 Python 中存在两种输出值的方法:表达式语句以及 print() ...

最新文章

  1. 升维:ValueError: all the input arrays must have same number of dimensions,
  2. android中各属性布局,Android里面各布局的属性定义
  3. json为全局变量 vue_vue package.json设置全局变量
  4. leetcode 290. 单词规律(Java版)
  5. ubuntu安装vscode的两种方法
  6. OCPC 广告算法在凤凰新媒体的实践探索
  7. Java LinkedHashMap的实现原理详解
  8. 硬盘显示容量和实际容量不符合_买移动固态硬盘纠结大半天?花2分钟看完这篇,购买时不再被坑...
  9. 暴走大侠显示进入服务器失败,暴走大侠:常见问题详解,再遇见这样的问题也不再迷糊...
  10. 界面设计方法 (1) — 3. 字典功能的设计
  11. VUE3 Router路由
  12. x61 linux 驱动 无线网卡,Linux环境Thinkpad X61 4G内存Mtrr表错误
  13. AndroidStudio_开发工具的设置_代码编辑器使用_新特性---Android原生开发工作笔记73
  14. 使用AfxGetMainWnd函数的一个心得
  15. NetApp S550:做“小”的艺术
  16. React中用aliplayer-react封装播放组件
  17. 在Arcgis中更改图层的坐标系
  18. 扇贝python多少钱_扇贝多少钱一斤?扇贝多少钱一斤2017?
  19. 别让这些考场突发情况毁了你一整年的心血!!
  20. 展示类页面测试Excel基础

热门文章

  1. matlab表达式部分项求和,matlab如何得到符号表达式中某一部分项的系数
  2. zabbix华为交换机模板_【教程】思科交换机镜像端口配置实例
  3. 【转】Asp.net的生命周期应用之IHttpModule和IHttpHandler
  4. SharePoint入门识记-整体架构
  5. .NET(C#)有哪些主流的ORM框架
  6. 设计模式(三)创建型模式
  7. 【Python 必会技巧】使用 Python 追加写入 json 文件或更改 json 文件中的值
  8. 简易有WEB文件服务器,Python实现简易版的Web服务器(推荐).pdf
  9. *【Hihocoder - offer编程练习赛94 - A】最短管道距离(中位数)
  10. oracle dump enq hw,经典故障分析 - ASSM引发的索引争用与 enq HW -contentio