C++小程序源码如何实现文件加密、压缩、解压、对比与排序?
在C++编程中,实现文件加密、压缩、解压、对比与排序等功能,可以帮助我们更好地管理和保护数据。以下将详细介绍这些功能的实现方法。
一、文件加密
文件加密是保护数据安全的重要手段。在C++中,我们可以使用AES加密算法实现文件加密。以下是一个简单的示例:
#include
#include
#include
#include
#include
void encrypt(const std::string& input_file, const std::string& output_file, const std::string& key) {
FILE* in = fopen(input_file.c_str(), "rb");
FILE* out = fopen(output_file.c_str(), "wb");
if (!in || !out) {
std::cerr << "Error opening file" << std::endl;
return;
}
unsigned char* buffer = new unsigned char[1024];
unsigned char* key_bytes = reinterpret_cast(const_cast(key.c_str()));
unsigned char* iv = new unsigned char[AES_BLOCK_SIZE];
RAND_bytes(iv, AES_BLOCK_SIZE);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key_bytes, iv);
while (true) {
size_t len;
int ret = fread(buffer, 1, 1024, in);
if (ret <= 0) break;
EVP_EncryptUpdate(ctx, buffer, &len, buffer, ret);
fwrite(buffer, 1, len, out);
}
EVP_EncryptFinal_ex(ctx, buffer, &len);
fwrite(buffer, 1, len, out);
fclose(in);
fclose(out);
delete[] buffer;
delete[] iv;
EVP_CIPHER_CTX_free(ctx);
}
int main() {
std::string input_file = "example.txt";
std::string output_file = "encrypted.txt";
std::string key = "1234567890123456";
encrypt(input_file, output_file, key);
return 0;
}
二、文件压缩
文件压缩可以减小文件大小,提高存储和传输效率。在C++中,我们可以使用zlib库实现文件压缩。以下是一个简单的示例:
#include
#include
#include
void compress(const std::string& input_file, const std::string& output_file) {
FILE* in = fopen(input_file.c_str(), "rb");
FILE* out = fopen(output_file.c_str(), "wb");
if (!in || !out) {
std::cerr << "Error opening file" << std::endl;
return;
}
unsigned char* buffer = new unsigned char[1024];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
if (deflateInit(&strm, 9) != Z_OK) {
std::cerr << "Error initializing zlib" << std::endl;
return;
}
while (true) {
strm.avail_in = fread(buffer, 1, 1024, in);
if (ferror(in)) {
std::cerr << "Error reading file" << std::endl;
break;
}
if (strm.avail_in == 0) break;
strm.next_in = reinterpret_cast(buffer);
do {
unsigned have;
if (strm.avail_out == 0) {
strm.avail_out = 1024;
strm.next_out = reinterpret_cast(buffer);
}
deflate(&strm, Z_NO_FLUSH);
have = strm.total_out - strm.avail_out;
if (have) fwrite(buffer, 1, have, out);
} while (strm.avail_out == 0);
}
deflateEnd(&strm);
fclose(in);
fclose(out);
delete[] buffer;
}
int main() {
std::string input_file = "example.txt";
std::string output_file = "compressed.txt";
compress(input_file, output_file);
return 0;
}
三、文件解压
文件解压是将压缩文件恢复到原始状态的过程。在C++中,我们可以使用zlib库实现文件解压。以下是一个简单的示例:
#include
#include
#include
void decompress(const std::string& input_file, const std::string& output_file) {
FILE* in = fopen(input_file.c_str(), "rb");
FILE* out = fopen(output_file.c_str(), "wb");
if (!in || !out) {
std::cerr << "Error opening file" << std::endl;
return;
}
unsigned char* buffer = new unsigned char[1024];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
if (inflateInit(&strm) != Z_OK) {
std::cerr << "Error initializing zlib" << std::endl;
return;
}
while (true) {
strm.avail_in = fread(buffer, 1, 1024, in);
if (ferror(in)) {
std::cerr << "Error reading file" << std::endl;
break;
}
if (strm.avail_in == 0) break;
strm.next_in = reinterpret_cast(buffer);
do {
unsigned have;
if (strm.avail_out == 0) {
strm.avail_out = 1024;
strm.next_out = reinterpret_cast(buffer);
}
inflate(&strm, Z_NO_FLUSH);
have = strm.total_out - strm.avail_out;
if (have) fwrite(buffer, 1, have, out);
} while (strm.avail_out == 0);
}
inflateEnd(&strm);
fclose(in);
fclose(out);
delete[] buffer;
}
int main() {
std::string input_file = "compressed.txt";
std::string output_file = "decompressed.txt";
decompress(input_file, output_file);
return 0;
}
四、文件对比
文件对比可以帮助我们检查两个文件是否相同。在C++中,我们可以使用标准库中的fstream和fstream_iterator实现文件对比。以下是一个简单的示例:
#include
#include
#include
bool compare_files(const std::string& file1, const std::string& file2) {
std::ifstream in1(file1);
std::ifstream in2(file2);
std::istream_iterator it1(in1), it2(in2);
return std::equal(it1, it1, it2);
}
int main() {
std::string file1 = "example1.txt";
std::string file2 = "example2.txt";
if (compare_files(file1, file2)) {
std::cout << "Files are equal" << std::endl;
} else {
std::cout << "Files are not equal" << std::endl;
}
return 0;
}
五、文件排序
文件排序可以帮助我们根据特定条件对文件进行排序。在C++中,我们可以使用标准库中的sort和vector实现文件排序。以下是一个简单的示例:
#include
#include
#include
#include
#include
bool compare_strings(const std::string& str1, const std::string& str2) {
return str1 < str2;
}
void sort_files(const std::string& input_file, const std::string& output_file) {
std::ifstream in(input_file);
std::vector lines;
std::string line;
while (std::getline(in, line)) {
lines.push_back(line);
}
std::sort(lines.begin(), lines.end(), compare_strings);
std::ofstream out(output_file);
for (const auto& str : lines) {
out << str << std::endl;
}
}
int main() {
std::string input_file = "example.txt";
std::string output_file = "sorted.txt";
sort_files(input_file, output_file);
return 0;
}
通过以上示例,我们可以看到在C++中实现文件加密、压缩、解压、对比与排序等功能的方法。在实际应用中,我们可以根据需求选择合适的方法,并对其进行优化和扩展。
猜你喜欢:短信验证码平台