AES.h头文件

#include <cstring>
#include <fstream>
#include <iostream>
#include <openssl/aes.h>

//AES文件加密函数
int aes_encrypt_file(const std::string &original_backup_file_path,
                     const std::string &encrypted_file_path,
                     const std::string &password) {
    //创建 ass_key 对象
    AES_KEY aes_key{};
    AES_set_encrypt_key(reinterpret_cast<const uint8_t*>(password.c_str()), 256, &aes_key);
    //定义加密块的大小,必须是16字节
    int encrypt_chunk_size = 16;
    std::ifstream original_backup_file(original_backup_file_path,std::ios::binary);
    std::ofstream encrypted_file(encrypted_file_path, std::ios::binary);
    char aes_encrypt_in[encrypt_chunk_size + 1];
    char aes_encrypt_out[encrypt_chunk_size +1];

    while (!original_backup_file.eof()) {
        original_backup_file.read(aes_encrypt_in, encrypt_chunk_size);
        if (original_backup_file.gcount() < encrypt_chunk_size) {
            encrypted_file.write(aes_encrypt_in, original_backup_file.gcount());
        } else {
            AES_encrypt(reinterpret_cast<const unsigned char*>(aes_encrypt_in),
                        reinterpret_cast<unsigned char*>(aes_encrypt_out),
                        &aes_key);
            encrypted_file.write(aes_encrypt_out, original_backup_file.gcount());
        }
    };

    encrypted_file.close();
    original_backup_file.close();
    return 0;
}

//AES文件解密函数
int aes_decrypt_file(const std::string &encrypted_file_path,
                     const std::string &decrypted_file_path,
                     const std::string &password) {
    AES_KEY aes_key{};
    AES_set_decrypt_key(reinterpret_cast<const uint8_t*>(password.c_str()), 256, &aes_key);
    int encrypt_chunk_size = 16;
    std::ifstream encrypted_file(encrypted_file_path, std::ios::binary);
    std::ofstream decrypted_file(decrypted_file_path, std::ios::binary);
    char aes_decrypt_in[encrypt_chunk_size + 1];
    char aes_decrypt_out[encrypt_chunk_size +1];
    while (!encrypted_file.eof()) {
        encrypted_file.read(aes_decrypt_in, encrypt_chunk_size);
        if (encrypted_file.gcount() < encrypt_chunk_size) {
            decrypted_file.write(aes_decrypt_in, encrypted_file.gcount());
        } else {
            AES_decrypt(reinterpret_cast<const unsigned char*>(aes_decrypt_in),
                        reinterpret_cast<unsigned char*>(aes_decrypt_out),
                        &aes_key);
            decrypted_file.write(aes_decrypt_out, encrypted_file.gcount());
        }
    };
    encrypted_file.close();
    decrypted_file.close();
    return 0;
}

main函数调用

#include "AES.h"

int main(){
    //Test AES.h enc
    std::string enc_from = "/home/gsc/Projects/1.txt";
    std::string enc_to_dec_from = "/home/gsc/Projects/2.enc";
    std::string dec_to ="/home/gsc/Projects/3.txt";
    std::string password = "Cxt123456";
     aes_encrypt_file(enc_from,enc_to_dec_from,password);
    //Test AES.h dec
    aes_decrypt_file(enc_to_dec_from,dec_to,password);
}

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐