-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
743 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## encrypt_decrypt | ||
|
||
可以对任何文件进行加密解密,实现对称加密,但用户无需记住任何密钥。 | ||
|
||
升级版:[双密钥二重加密](https://github.com/AimTao/tool-by-cpp/tree/master/executable/encrypt_decrypt_double) | ||
|
||
|
||
|
||
## 随机分组加密 | ||
|
||
1. 获取系统当前时间,处理后得到随机密钥、随机分组大小。 | ||
2. 混淆系统当前时间,分块写入加密文件。 | ||
3. 利用密钥加密明文。 | ||
|
||
|
||
|
||
## 使用 | ||
|
||
程序将根据文件名,自动生成 加密后的文件名 / 解密后的文件名。例如: | ||
|
||
1. 待加密的文件:wallpaper.png 加密后的文件:wallpaper_enc.png | ||
2. 待解密的文件:wallpaper.png 解密后的文件:wallpaper_dec.png | ||
|
||
### 命令行式 | ||
|
||
+ linux、mac | ||
|
||
```sh | ||
# 加密 | ||
./enc need_encrypt_file | ||
|
||
#解密 | ||
./dec need_decrypt_file | ||
``` | ||
|
||
+ Windows | ||
|
||
```sh | ||
# 加密 | ||
enc need_encrypt_file | ||
|
||
#解密 | ||
dec need_decrypt_file | ||
``` | ||
|
||
### 交互式 | ||
|
||
双击运行图标运行。 | ||
|
||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int main(int argc, char **argv) { | ||
|
||
// 读入待解密文件路径 | ||
char *enc_path = argv[1]; | ||
char backup_enc_path[512] = {0}; | ||
strcpy(backup_enc_path, enc_path); | ||
|
||
// 打开读文件,检查路径 | ||
FILE *enc_fp = NULL; | ||
enc_fp = fopen(enc_path, "rb+"); | ||
if (enc_fp == NULL) { | ||
perror("\n【打开待解密文件失败!】\n 原因:"); | ||
return 0; | ||
} | ||
|
||
// 生成解密文件路径 | ||
char dec_path[512] = {0}; | ||
char last_name[100] = {0}; // 获取文件后缀名 | ||
char file_name[100] = {0}; // 获取文件名(省去后缀名) | ||
char *strtok_p = NULL; | ||
strtok_p = strtok(backup_enc_path, "."); | ||
strtok_p = strtok(NULL, "."); | ||
if (strtok_p != NULL) { | ||
while (strtok_p != NULL) { | ||
strcpy(last_name, strtok_p); | ||
strtok_p = strtok(NULL, "."); | ||
} | ||
char *last_path_ptr = NULL; | ||
char *enc_path_ptr = enc_path; | ||
while (1) { | ||
last_path_ptr = strstr(enc_path_ptr, last_name); | ||
if (last_path_ptr == NULL) { | ||
enc_path_ptr -= strlen(last_name); | ||
break; | ||
} else { | ||
enc_path_ptr = last_path_ptr + strlen(last_name); | ||
if (enc_path_ptr > enc_path + strlen(enc_path)) { | ||
break; | ||
} | ||
} | ||
} | ||
strncpy(file_name, enc_path, enc_path_ptr - 1 - enc_path); | ||
sprintf(dec_path, "%s_dec.%s", file_name, last_name); | ||
} else { | ||
sprintf(dec_path, "%s_dec", enc_path); | ||
} | ||
|
||
// 打开写文件 | ||
FILE *dec_fp = NULL; | ||
dec_fp = fopen(dec_path, "wb+"); | ||
if (dec_fp == NULL) { | ||
perror("\n【解密失败!】\n 原因:"); | ||
return 0; | ||
} | ||
|
||
// 读取密钥 | ||
long num_key = 0; | ||
fread(&num_key, 1, sizeof(num_key), enc_fp); | ||
char key[100] = {0}; | ||
sprintf(key, "%ld", num_key * 13 - 20200502); | ||
int key_lens = strlen(key); | ||
int size_key = num_key % 5 * 1024 + 1024; | ||
|
||
// 边读边写,解密 | ||
while (1) { | ||
char tmp_enc[6144] = {0}; // 1024 * 5 | ||
int read_size = fread(tmp_enc, 1, size_key, enc_fp); | ||
if (!read_size) { | ||
break; | ||
} | ||
for (int i = 0; i < read_size; i++) { | ||
tmp_enc[i] = tmp_enc[i] ^ key[i % key_lens]; | ||
} | ||
fwrite(tmp_enc, 1, read_size, dec_fp); | ||
} | ||
printf("\n\n解密已完成!解密文件位于 %s\n\n", dec_path); | ||
|
||
// 关闭文件 | ||
fclose(enc_fp); | ||
fclose(dec_fp); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
int main(int argc, char **argv) { | ||
|
||
// 获取待加密文件路径 | ||
char *source_path = argv[1]; | ||
char backup_source_path[512] = {0}; | ||
strcpy(backup_source_path, source_path); | ||
|
||
// 打开读文件,检查路径 | ||
FILE *source_fp = NULL; | ||
source_fp = fopen(source_path, "rb"); | ||
if (source_fp == NULL) { | ||
perror("\n【打开待加密文件失败!】\n 原因:"); | ||
return 0; | ||
} | ||
|
||
// 生成已加密文件路径 | ||
char enc_path[512] = {0}; | ||
char last_name[100] = {0}; // 获取文件后缀名 | ||
char file_name[100] = {0}; // 获取文件名(省去后缀名) | ||
char *strtok_p = strtok(backup_source_path, "."); | ||
strtok_p = strtok(NULL, "."); | ||
if (strtok_p != NULL) { | ||
while (strtok_p != NULL) { | ||
strcpy(last_name, strtok_p); | ||
strtok_p = strtok(NULL, "."); | ||
} | ||
char *last_path_ptr = NULL; | ||
char *source_path_ptr = source_path; | ||
while (1) { | ||
last_path_ptr = strstr(source_path_ptr, last_name); | ||
if (last_path_ptr == NULL) { | ||
source_path_ptr -= strlen(last_name); | ||
break; | ||
} else { | ||
source_path_ptr = last_path_ptr + strlen(last_name); | ||
if (source_path_ptr > source_path + strlen(source_path)) { | ||
break; | ||
} | ||
} | ||
} | ||
strncpy(file_name, source_path, source_path_ptr - 1 - source_path); | ||
sprintf(enc_path, "%s_enc.%s", file_name, last_name); | ||
} else { | ||
sprintf(enc_path, "%s_enc", source_path); | ||
} | ||
|
||
// 打开写文件 | ||
FILE *enc_fp = NULL; | ||
enc_fp = fopen(enc_path, "wb+"); | ||
if (enc_fp == NULL) { | ||
perror("\n【加密失败!】\n 原因:"); | ||
return 0; | ||
} | ||
|
||
// 用当前时间做随机量,设计随机密钥、随机块大小 | ||
long num_key = time(NULL); | ||
char key[100] = {0}; | ||
sprintf(key, "%ld", num_key * 13 - 20200502); | ||
int key_lens = strlen(key); | ||
int size_key = num_key % 5 * 1024 + 1024; | ||
// 写入解密密钥 | ||
fwrite(&num_key, 1, sizeof(num_key), enc_fp); | ||
|
||
// 边读边写,加密 | ||
while (1) { | ||
char tmp_source[6144] = {0}; // 1024 * 5 | ||
int read_size = fread(tmp_source, 1, size_key, source_fp); | ||
if (!read_size) { | ||
break; | ||
} | ||
for (int i = 0; i < read_size; i++) { | ||
tmp_source[i] = tmp_source[i] ^ key[i % key_lens]; | ||
} | ||
fwrite(tmp_source, 1, read_size, enc_fp); | ||
} | ||
printf("\n加密已完成!加密文件位于 %s\n\n", enc_path); | ||
|
||
// 关闭文件 | ||
fclose(source_fp); | ||
fclose(enc_fp); | ||
|
||
return 0; | ||
} | ||
|
Oops, something went wrong.