突然遇到一个加密需求和解密需求

下面是gpt回答的

1.libarchive 使用密码压缩文件和解压缩文件

#include <archive.h>
#include <archive_entry.h>
#include <stdio.h>
#include <string.h>

int create_encrypted_archive(const char* input_dir, const char* output_filename, const char* password) {
    struct archive* a;
    struct archive_entry* entry;
    int r;

    a = archive_write_new();
    archive_write_set_format_zip(a);
    archive_write_set_format_option(a, "zip", "encryption", "aes256");
    archive_write_set_format_option(a, "zip", "encpassword", password);
    archive_write_open_filename(a, output_filename);

    entry = archive_entry_new();
    archive_entry_copy_pathname(entry, input_dir);
    archive_entry_set_filetype(entry, AE_IFDIR);
    archive_write_header(a, entry);

    archive_entry_free(entry);
    archive_write_free(a);
    return 0;
}

int extract_encrypted_archive(const char* input_filename, const char* output_dir, const char* password) {
    struct archive* a;
    struct archive_entry* entry;
    int r;

    a = archive_read_new();
    archive_read_support_format_zip(a);
    archive_read_support_filter_all(a);
    archive_read_add_passphrase(a, password);
    r = archive_read_open_filename(a, input_filename, 10240);
    if (r != ARCHIVE_OK) {
        return -1;
    }

    while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
        const char* filename = archive_entry_pathname(entry);
        char output_path[1024];
        snprintf(output_path, sizeof(output_path), "%s/%s", output_dir, filename);

        if (archive_entry_filetype(entry) == AE_IFREG) {
            FILE* f = fopen(output_path, "wb");
            if (!f) {
                return -1;
            }

            const size_t bufsize = 10240;
            char buf[bufsize];
            ssize_t len;
            while ((len = archive_read_data(a, buf, bufsize)) > 0) {
                fwrite(buf, 1, len, f);
            }
            fclose(f);
        } else if (archive_entry_filetype(entry) == AE_IFDIR) {
            mkdir(output_path, 0777);
        }
    }

    archive_read_close(a);
    archive_read_free(a);
    return 0;
}

int main() {
    const char* password = "MySecretPassword";
    const char* input_dir = "/path/to/input/dir";
    const char* output_filename = "/path/to/output/archive.zip";
    const char* output_dir = "/path/to/output/dir";

    // Create the encrypted archive
    create_encrypted_archive(input_dir, output_filename, password);

    // Extract the contents of the encrypted archive
    extract_encrypted_archive(output_filename, output_dir, password);

    return 0;
}

2.openssl aes加密解密

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

using namespace std;

// AES加密函数
void AESEncrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
    AES_KEY aes_key;
    AES_set_encrypt_key(key, 128, &aes_key);
    AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT);
}

// AES解密函数
void AESDecrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
    AES_KEY aes_key;
    AES_set_decrypt_key(key, 128, &aes_key);
    AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aes_key, iv, AES_DECRYPT);
}

int main() {
    // 生成随机密钥
    unsigned char key[AES_BLOCK_SIZE];
    srand((unsigned int)time(NULL));
    for (int i = 0; i < AES_BLOCK_SIZE; i++) {
        key[i] = rand() % 256;
    }

    // 初始化IV向量
    unsigned char iv[AES_BLOCK_SIZE];
    memset(iv, 0, AES_BLOCK_SIZE);

    // 读取需要加密的文件
    string filename = "test.txt";
    ifstream fin(filename.c_str(), ios::binary);
    if (!fin) {
        cout << "Failed to open file: " << filename << endl;
        return -1;
    }
    fin.seekg(0, ios::end);
    int file_len = fin.tellg();
    fin.seekg(0, ios::beg);
    char *buf = new char[file_len];
    fin.read(buf, file_len);
    fin.close();

    // 加密文件内容
    unsigned char *ciphertext = new unsigned char[file_len + AES_BLOCK_SIZE];
    AESEncrypt((unsigned char *)buf, file_len, key, iv, ciphertext);

    // 保存加密后的文件
    string output_filename = "test_encrypted.txt";
    ofstream fout(output_filename.c_str(), ios::binary);
    if (!fout) {
        cout << "Failed to create file: " << output_filename << endl;
        delete[] buf;
        delete[] ciphertext;
        return -1;
    }
    fout.write((char *)key, AES_BLOCK_SIZE);
    fout.write((char *)ciphertext, file_len + AES_BLOCK_SIZE);
    fout.close();
    delete[] buf;
    delete[] ciphertext;

    // 读取加密后的文件
    fin.open(output_filename.c_str(), ios::binary);
    if (!fin) {
        cout << "Failed to open file: " << output_filename << endl;
        return -1;
    }
    fin.seekg(0, ios::end);
    int encrypted_file_len = fin.tellg();
    fin.seekg(0, ios::beg);
    unsigned char *encrypted_buf = new unsigned char[encrypted_file_len];
    fin.read((char *)encrypted_buf, encrypted_file_len);
    fin.close();

    // 解密文件内容
    unsigned char *decrypted_buf = new unsigned char[encrypted_file_len - AES_BLOCK_SIZE];
    AESDecrypt(encrypted_buf + AES_BLOCK_SIZE, encrypted_file_len - AES_BLOCK_SIZE, encrypted_buf, iv, decrypted_buf);

    // 保存解

posted @ 2023-05-12 17:01  simp00  阅读(136)  评论(0编辑  收藏  举报