1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Ubuntu环境下 使用clion编译器 使用开源opensll的对称AES算法对于文件进行加密 C++代码

Ubuntu环境下 使用clion编译器 使用开源opensll的对称AES算法对于文件进行加密 C++代码

时间:2021-03-20 04:26:55

相关推荐

Ubuntu环境下 使用clion编译器 使用开源opensll的对称AES算法对于文件进行加密 C++代码

前提准备条件

需要安装openssl需要安装openssl-dev需要配置CMakeLists.txt文件集体内容可以参考我提供的相关参考链接

AES_file.h

#include <openssl/aes.h>#include <iostream>#include <fstream>#include <cstring>#define RELEASE_ARRAY(P) if (P) \{ \delete[] P; \P = NULL;\}class AES{public:// AES文件加密函数 ///int TestAesEncryptFile(std::string original_backup_file_path, std::string backup_dir_path, std::string &password){int encrypt_chunk_size = 16;std::ifstream original_backup_file(original_backup_file_path.c_str(), std::ios::binary);std::ofstream backup_dir(backup_dir_path, std::ios::binary);if (!original_backup_file){std::cout << "Can not open original_backup_file file." << std::endl;return 1;}if (!backup_dir){std::cout << "Can not open backup_dir_path file." << std::endl;return 1;}//用指定密钥对一段内存进行加密,结果放在outbuffer中unsigned char aes_keybuf[32];memset(aes_keybuf, 0, sizeof(aes_keybuf));strcpy((char *)aes_keybuf, password.c_str());AES_KEY aeskey;AES_set_encrypt_key(aes_keybuf, 256, &aeskey);char *in_data = new char[encrypt_chunk_size + 1];char *out_data = new char[encrypt_chunk_size + 1];while (!original_backup_file.eof()){original_backup_file.read(in_data, encrypt_chunk_size);if (original_backup_file.gcount() < encrypt_chunk_size){backup_dir.write(in_data, original_backup_file.gcount());}else{AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);backup_dir.write(out_data, original_backup_file.gcount());}};backup_dir.close();original_backup_file.close();RELEASE_ARRAY(in_data);RELEASE_ARRAY(out_data);return 0;}// AES文件解密函数 //int TestAesDecryptFile(std::string in_file_path, std::string out_file_path, std::string &password){int encrypt_chunk_size = 16;std::ifstream original_backup_file(in_file_path.c_str(), std::ios::binary);std::ofstream backup_dir(out_file_path, std::ios::binary);if (!original_backup_file){std::cout << "Can not open original_backup_file file." << std::endl;return 1;}if (!backup_dir){std::cout << "Can not open backup_dir file." << std::endl;return 1;}//用指定密钥对一段内存进行加密,结果放在outbuffer中unsigned char aes_keybuf[32];memset(aes_keybuf, 0, sizeof(aes_keybuf));strcpy((char *)aes_keybuf, password.c_str());AES_KEY aeskey;AES_set_decrypt_key(aes_keybuf, 256, &aeskey);char *in_data = new char[encrypt_chunk_size + 1];char *out_data = new char[encrypt_chunk_size + 1];while (!original_backup_file.eof()){original_backup_file.read(in_data, encrypt_chunk_size);if (original_backup_file.gcount() < encrypt_chunk_size){backup_dir.write(in_data, original_backup_file.gcount());}else{AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);backup_dir.write(out_data, original_backup_file.gcount());}};backup_dir.close();original_backup_file.close();RELEASE_ARRAY(in_data);RELEASE_ARRAY(out_data);return 0;}};

main.cpp

#include <iostream>#include "load_or_save_db.h"#include "include/AES_file.h"int main() {time_t t1, t2, t3, t4;t1 = time(nullptr);printf("加解密起始时间: %s\n", ctime(&t1));std::string password = "xcdf123456";AES aes;// 数据开始加密aes.TestAesEncryptFile("/home/gsc/Projects/1.txt", "/home/gsc/Projects/2.txt", password);t2 = time(nullptr);printf("AES256加密成功!\n");printf("加密用时: %ld秒\n", (t2 - t1));t3 = time(NULL);// 数据开始解密aes.TestAesDecryptFile("/home/gsc/Projects/2.txt", "/home/gsc/Projects/3.txt", password);t4 = time(NULL);printf("AES256解密成功!\n");printf("解密用时: %lld秒\n", (t4 - t3));return 0;}

参考链接

C++: 基于OpenSSL的AES256加解密测试clion中链接openssl库

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