1、fseek,ftell,fread,fwrite(简单文件加密)
#define
_crt_secure_no_warnings
//去掉安全检查
#include
<stdio.h>
<stdlib.h>
/*读取文件大小*/
int
getfilesize(char
*path)
{
file *pf
= fopen(path,
"r");
if (pf
== null)
fclose(pf);
return -1;
}
else
fseek(pf,0,seek_end);
length =
ftell(pf);
//获取文件大小
return
length;
/*实现文件复制*/
void
copy(char
*oldpath,char
*newpath)
file *pfr,
*pfw;
//以打开二进制文件的方式打开
pfr =
fopen(oldpath,
"rb");
//写入二进制模式
pfw =
fopen(newpath,
"wb");
if (pfr
== null ||
pfw ==
null)
fclose(pfr);
//关闭文件
fclose(pfw);
return;
getfilesize(oldpath);
//分配内存,读取文件
char *p
= (char *)malloc(length
* sizeof(char));
//读取二进制到内存
fread(p,sizeof(char),length,pfr);
//写入二进制到文件
fwrite(p,sizeof(char),length,pfw);
fclose(pfr);
encryptyfile(char
//关闭文件
= (char *)malloc(length*sizeof(char));
for (int
i = 0;
i <
length;i++)
//加密方法是,与制定字符进行异或操作
p[i]
^= 'a';
fwrite(p,
sizeof(char),
length,
pfw);
/*解密文件*/
decodefile(char
fopen(newpath,"wb");
i;
for (i
= 0; i <
{
}
main(int
argc,char
*argv[])
char *path1
= "g:\\1.txt";
char *path2
= "g:\\2.txt";
char *path3
= "g:\\3.txt";
encryptyfile(path1,
path2);
decodefile(path2,
path3);
//printf("%d\n",getfilesize(path1));
system("pause");
return 0;
上面的过程将会把加解密的文件输出到文件中。
_crt_secure_no_warnings
char
jiami(char
ch)
ch ^ 123;
jiemi(char
jia(char
*path,
char *pathjia)
fopen(path,
"r");//读取
fopen(pathjia,
"w");//写入
//feof(file *pf)
到了文件末尾1,没有到就是0
//下面的过程将把文件内容输出到屏幕上。
while (!feof(pfr))
//读取字符
ch =
fgetc(pfr);
putchar(ch);
//输出字符的时候将字符加密
fputc(jiami(ch),
fclose(pfw);//关闭文件
/*解密*/
jie(char
*path,char
*pathjie)
//读取
//写入
fopen(pathjie,"w");
//到了文件末尾1,没有到就是0
char
fgetc(pfr);//读取字符
fputc(jiemi(ch),
pfw);//写入一个加密的字符
//jia(path1, path2);
jie(path2,
密码加密
头文件:
#include<stdlib.h>
#include<string.h>
//字符串加密
char *
stringjiami(char
*password,
char *string);
//字符串解密
stringjiemi(char
char *jiastring);
filejiami(char
char *pathjia,
char *password);
filejiemi(char
*pathjia,
char *pathjie,
"encrytiondecrytion.h"
= fopen(path,"r");
fseek(pf,
0, seek_end);//文件末尾
length;//返回长度
char *string)
//获取加密长度
passlength =
strlen(password);
//获取字符串长度
stringlength =
strlen(string);
if (stringlength
% passlength == 0)
//循环次数
ci =
stringlength /
passlength;
i,j;
ci;i++)
//循环密码
for (j
= 0; j <
passlength;j++)
string[passlength*i
+ j] ^=
password[j];
i,
j;
ci;i++)
string[passlength
* i +
j] ^=
lastlength =
stringlength %
passlength;//剩下的长度
lastlength;i++)
string[passlength*(stringlength
/ passlength) +
i] ^=
password[i];
string;
char *jiastring)
strlen(jiastring);
%passlength == 0)
passlength;//循环次数
ci;
i++)
//异或加密
jiastring[passlength
//异或加密
jiastring[passlength*i
//剩下的长度
j <
jiastring[passlength*(stringlength
jiastring;
char *password)
fopen(pathjia,"w");
getfilesize(path);
char *newstr
= (char*)malloc(sizeof(char)*(length
+ 1));
fgetc(pfr);
//获取一个字符
newstr[i]
= ch;//不断存入字符
//字符串处理为'\0'
newstr[length]
= '\0';
//加密字符串
stringjiami(password,
newstr);
//挨个写入字符
fputc(newstr[i],
main.c
string[50] =
"锄禾日当午";
char *password
= "123";
printf("%s\n",stringjiami(password,string));
printf("%s\n",
string));
filejiami(path1,
path2,
"abcde");
filejiami(path2,
path3,
fscanf
#include<stdio.h>
argc,
char *argv[])
str[100] = { 0 };
fscanf(stdin,
"%s",
str);
fprintf(stdout,
"str=%s\n",
system(str);