我创建了两个C函数来加密和解密整个文件夹。
#include "myaes.h"
#define SUFFIX ".crypt" //the file suffix for encrypted files
#define MASTER "6ADAC6D678CF49EEF4BB9C16A61F4"
int aesEncryptDir(const char *d)
//encrypts every file in directory d
{
AES_KEY key;
DIR *dp = opendir(d);
FILE *fp,*fp_c;
struct dirent *entry;
char fullpath[strlen(d) + 300];
char cryppath[strlen(fullpath) + 20];
char buffer[10000];
void *in = malloc(16);
void *out = malloc(16);
int sz;
size_t s_read;
if(AES_set_encrypt_key(MASTER,256,&key) != 0)
return -1;
const AES_KEY *static_key = &key;
if(!dp) //could not dir file pointer, maybe the folder does not exist
return -1;
while(entry = readdir(dp))
{
if(strncmp(entry->d_name,".",1) != 0 && strncmp(entry->d_name,"..",2) != 0)
{
memset(fullpath,0,sizeof(fullpath));
memset(cryppath,0,sizeof(cryppath));
memset(buffer,0,sizeof(buffer));
snprintf(fullpath,sizeof(fullpath),"%s/%s",d,entry->d_name);
snprintf(cryppath,sizeof(fullpath),"%s%s",fullpath,SUFFIX);
if(strstr(fullpath,SUFFIX) != NULL)
continue;
fp = fopen(fullpath,"r+");
fp_c = fopen(cryppath,"w");
fseek(fp,0L,SEEK_END);
sz = ftell(fp);
rewind(fp);
fwrite(&sz,sizeof(int),1,fp_c);
while((s_read = fread(buffer,1,1000,fp)))
{
for(int v = 0; v < s_read; v+= 16)
{
memcpy(in,buffer+v,16);
AES_encrypt(in,out,static_key);
fwrite(out,1,16,fp_c);
}
}
}
fclose(fp);
remove(fullpath);
fclose(fp_c);
}
if(closedir(dp) != 0)
return -1;
free(in);
free(out);
return 0;
}
int aesDecryptDir(const char *d)
//decrypts every file in directory d
{
AES_KEY key;
DIR *dp = opendir(d);
FILE *fp,*fp_c;
struct dirent *entry;
char fullpath[strlen(d) + 300];
char comppath[strlen(d) + 300];
char recoverpath[strlen(fullpath)];
char buffer[10000];
int sz = 0;
void *in = malloc(16);
void *out= malloc(16);
if(AES_set_decrypt_key(MASTER,256,&key) != 0)
return -1;
const AES_KEY *static_key = &key;
if(!dp) //could not dir file pointer, maybe the folder does not exist
return -1;
while(entry = readdir(dp))
{
if(strncmp(entry->d_name,".",1) != 0 && strncmp(entry->d_name,"..",2) != 0)
{
memset(fullpath,0,sizeof(fullpath));
memset(recoverpath,0,sizeof(recoverpath));
snprintf(fullpath,sizeof(fullpath),"%s/%s",d,entry->d_name);
strcpy(comppath,fullpath);
if(strstr(comppath,SUFFIX) == NULL)
continue;
strcpy(recoverpath,fullpath);
recoverpath[strlen(fullpath) - strlen(SUFFIX)] = '\0';
fp = fopen(fullpath,"r+");
fp_c = fopen(recoverpath,"w");
fread(&sz,sizeof(int),1,fp);
while(fread(in,16,1,fp))
{
AES_decrypt(in,out,static_key);
fwrite(out,(sz>=16)?16:sz,1,fp_c);
sz-=16;
}
fclose(fp);
fclose(fp_c);
}
}
if(closedir(dp) != 0)
return -1;
free(in);
free(out);
return 0;
}
myaes.h
#ifndef MYAES_H
#define MYAES_H
#include <dirent.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int aesEncryptDir(const char *d);
int aesDecryptDir(const char *d);
#endif
如果我现在通过aesEncryptDir和aesDecryptDir运行纯文本文件,它将保持不变。差异也表示没有变化。然而,二进制文件(.jpg)在此过程中会被破坏。
为什么会这样?它与二进制文件的编码有关吗?我是否必须以不同的方式处理二进制文件以进行加密/解密?