下面是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_encrypted_archive(input_dir, output_filename, password); |
| |
| |
| 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; |
| |
| |
| 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); |
| } |
| |
| |
| 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; |
| } |
| |
| |
| 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); |
| |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
2020-05-12 Qt 控件大小随着窗口的大小而变化设置