300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 使用openssl开源AES算法 实现aes aes-cbc和aes-ecb对字符串的加解密

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

时间:2020-11-23 13:56:08

相关推荐

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

注意事项

对于用户输入的密码进行了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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。