This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.c
64 lines (58 loc) · 1.7 KB
/
math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int Mode;
extern char *secretKey;
//临时用参数输入接口
void option_in()
{
printf("inpun Mode\n");
scanf("%d",&Mode);
printf("inpun secret\n");
scanf("%s",&secretKey);
}
//算法选择接口
void select(char *sourcefile,char *targetFile,char *secretKey,int Mode)
{
if(Mode==1)
{
if(encryptFile(sourcefile,targetFile,secretKey,Mode)){
return 1;}
}
//if(Mode==2)
//{
//if(encryptFile(fpSource,fpTarget))
// return 1;
//}
}
int encryptFile(char *sourcefile,char *targetFile,char *secretKey,char *Mode)
{FILE *fpSource, *fpTarget; // 要打开的文件的指针
char buffer[21]; // 缓冲区,用于存放从文件读取的数据
int readCount, // 每次从文件中读取的字节数
keyLen = strlen(secretKey), // 密钥的长度
i; // 循环次数
// 以二进制方式读取/写入文件
fpSource = fopen(sourcefile, "rb");
if(fpSource==NULL){
printf("[%s] open failed\n", sourcefile);
return 0;
}
fpTarget = fopen(targetFile, "wb");
if(fpTarget==NULL){
printf("文件[%s] write filed\n", fpTarget);
return 0;
}
// 不断地从文件中读取 keyLen 长度的数据,保存到buffer,直到文件结束
while( (readCount=fread(buffer, 1, keyLen, fpSource)) > 0 ){
// 对buffer中的数据逐字节进行异或运算
for(i=0; i<readCount; i++){
buffer[i] ^= secretKey[i];
}
// 将buffer中的数据写入文件
fwrite(buffer, 1, readCount, fpTarget);
}
fclose(fpSource);
fclose(fpTarget);
return 1;
}