写一个好用的linux文本io加密/解密代码
时间: 2024-01-12 10:05:44 浏览: 23
以下是一个较为完整的、好用的Linux文本IO加密/解密代码的示例,使用了openssl库中的AES算法:
```c
#include <openssl/aes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEY_SIZE 256
#define BLOCK_SIZE 16
#define BUFFER_SIZE 1024
void print_error(const char *msg) {
printf("Error: %s\n", msg);
}
void print_usage(const char *prog_name) {
printf("Usage: %s <encrypt/decrypt> <input file> <output file> <key>\n", prog_name);
}
int encrypt_file(const char *in_filename, const char *out_filename, const char *key_str) {
unsigned char key[KEY_SIZE/8];
AES_KEY aes_key;
int bytes_read, bytes_written;
unsigned char in_data[BLOCK_SIZE];
unsigned char out_data[BLOCK_SIZE];
FILE *in_file = fopen(in_filename, "rb");
FILE *out_file = fopen(out_filename, "wb");
if (!in_file) {
print_error("Could not open input file for reading.");
return -1;
}
if (!out_file) {
print_error("Could not open output file for writing.");
fclose(in_file);
return -1;
}
// Convert key string to binary
memset(key, 0, sizeof(key));
memcpy(key, key_str, strlen(key_str));
// Set encryption key
AES_set_encrypt_key(key, KEY_SIZE, &aes_key);
// Encrypt file
while ((bytes_read = fread(in_data, 1, BLOCK_SIZE, in_file)) > 0) {
AES_encrypt(in_data, out_data, &aes_key);
bytes_written = fwrite(out_data, 1, bytes_read, out_file);
if (bytes_written != bytes_read) {
print_error("Error writing encrypted data.");
fclose(in_file);
fclose(out_file);
return -1;
}
}
fclose(in_file);
fclose(out_file);
return 0;
}
int decrypt_file(const char *in_filename, const char *out_filename, const char *key_str) {
unsigned char key[KEY_SIZE/8];
AES_KEY aes_key;
int bytes_read, bytes_written;
unsigned char in_data[BLOCK_SIZE];
unsigned char out_data[BLOCK_SIZE];
FILE *in_file = fopen(in_filename, "rb");
FILE *out_file = fopen(out_filename, "wb");
if (!in_file) {
print_error("Could not open input file for reading.");
return -1;
}
if (!out_file) {
print_error("Could not open output file for writing.");
fclose(in_file);
return -1;
}
// Convert key string to binary
memset(key, 0, sizeof(key));
memcpy(key, key_str, strlen(key_str));
// Set decryption key
AES_set_decrypt_key(key, KEY_SIZE, &aes_key);
// Decrypt file
while ((bytes_read = fread(in_data, 1, BLOCK_SIZE, in_file)) > 0) {
AES_decrypt(in_data, out_data, &aes_key);
bytes_written = fwrite(out_data, 1, bytes_read, out_file);
if (bytes_written != bytes_read) {
print_error("Error writing decrypted data.");
fclose(in_file);
fclose(out_file);
return -1;
}
}
fclose(in_file);
fclose(out_file);
return 0;
}
int encrypt_decrypt_file(const char *mode, const char *in_filename, const char *out_filename, const char *key_str) {
if (strcmp(mode, "encrypt") == 0) {
return encrypt_file(in_filename, out_filename, key_str);
} else if (strcmp(mode, "decrypt") == 0) {
return decrypt_file(in_filename, out_filename, key_str);
} else {
print_error("Invalid mode. Must be 'encrypt' or 'decrypt'.");
return -1;
}
}
int main(int argc, char *argv[]) {
if (argc != 5) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
const char *mode = argv[1];
const char *in_filename = argv[2];
const char *out_filename = argv[3];
const char *key_str = argv[4];
int ret = encrypt_decrypt_file(mode, in_filename, "tmp.txt", key_str);
if (ret != 0) {
return EXIT_FAILURE;
}
// Rename temporary file to output file
ret = rename("tmp.txt", out_filename);
if (ret != 0) {
print_error("Could not rename temporary file.");
remove("tmp.txt");
return EXIT_FAILURE;
}
printf("File %s %sed successfully.\n", in_filename, mode);
return EXIT_SUCCESS;
}
```
这个例子中,我们使用了256位的AES算法,并将加密和解密操作封装到`encrypt_file`和`decrypt_file`函数中。我们还实现了一个`encrypt_decrypt_file`函数来根据传递的模式(加密或解密)选择相应的函数来处理文件。我们还实现了一个完整的命令行接口,可以通过传递加密/解密模式、输入文件、输出文件和密钥来使用这些函数加密和解密文件。在加密和解密过程中,我们还加入了错误处理和文件IO操作的检查,使得代码更加健壮和可靠。