文件加解密,文件操作

来源:互联网 发布:js中可以实现接口继承 编辑:程序博客网 时间:2024/05/03 12:27


1fseek,ftell,fread,fwrite(简单文件加密)

#define_CRT_SECURE_NO_WARNINGS //去掉安全检查

#include<stdio.h>

#include<stdlib.h>

 

/*读取文件大小*/

intgetfilesize(char *path)

{

   FILE *pf = fopen(path,"r");

   if (pf == NULL)

   {

       fclose(pf);

       return -1;

   }

   else

   {

       fseek(pf,0,SEEK_END);

       intlength =ftell(pf);

       //获取文件大小

       returnlength;

   }

   fclose(pf);

}

 

/*实现文件复制*/

voidcopy(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;

   }

   else

   {

       intlength =getfilesize(oldpath);

       //分配内存,读取文件

       char *p = (char *)malloc(length * sizeof(char));

       //读取二进制到内存

       fread(p,sizeof(char),length,pfr);

       //写入二进制到文件

       fwrite(p,sizeof(char),length,pfw);

       

       //关闭文件

       fclose(pfr);

       fclose(pfw);

   }

}

 

voidencryptyfile(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;

   }

   else

   {

       intlength =getfilesize(oldpath);

       //分配内存,读取文件

       char *p = (char *)malloc(length*sizeof(char));

       //读取二进制到内存

       fread(p,sizeof(char),length,pfr);

       for (inti = 0;i <length;i++)

       {

           //加密方法是,与制定字符进行异或操作

           p[i] ^= 'A';

       }

       //写入二进制到文件

       fwrite(p,sizeof(char),length,pfw);

 

       //关闭文件

       fclose(pfr);

       fclose(pfw);

   }

}

 

/*解密文件*/

voiddecodefile(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;

   }

   else

   {

       intlength =getfilesize(oldpath);

       //分配内存,读取文件

       char *p = (char *)malloc(length*sizeof(char));

       //读取二进制到内存

       fread(p,sizeof(char),length,pfr);

       inti;

       for (i = 0; i <length;i++)

       {

           p[i] ^= 'A';

       }

       //写入二进制到文件

       fwrite(p,sizeof(char),length,pfw);

   }

   //关闭文件

   fclose(pfr);

   fclose(pfw);

}

intmain(intargc,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;

}

上面的过程将会把加解密的文件输出到文件中。

#define_CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

 

charjiami(charch)

{

   returnch ^ 123;

}

 

charjiemi(charch)

{

   returnch ^ 123;

}

 

voidjia(char *path,char *pathjia)

{

   FILE *pfr, *pfw;

   pfr =fopen(path,"r");//读取

   pfw =fopen(pathjia,"w");//写入

   if (pfr == NULL ||pfw ==NULL)

   {

       fclose(pfw);

       fclose(pfr);

       return;

   }

   else

   {

       //feof(FILE *pf)到了文件末尾1,没有到就是0

       //下面的过程将把文件内容输出到屏幕上。

       while (!feof(pfr))

       {

           //读取字符

           charch =fgetc(pfr);

           putchar(ch);

           //输出字符的时候将字符加密

           fputc(jiami(ch),pfw);

       }

   }

   fclose(pfr);

   fclose(pfw);//关闭文件

}

 

/*解密*/

voidjie(char *path,char *pathjie)

{

   FILE *pfr, *pfw;

   //读取

   pfr =fopen(path,"r");

   //写入

   pfw =fopen(pathjie,"w");

   if (pfr == NULL ||pfw ==NULL)

   {

       fclose(pfr);

       fclose(pfw);

       return;

   }

   else

   {

       //到了文件末尾1,没有到就是0

       while (!feof(pfr))

       {

           char ch =fgetc(pfr);//读取字符

           putchar(ch);

           fputc(jiemi(ch),pfw);//写入一个加密的字符

       }

   }

   fclose(pfr);

   //关闭文件

   fclose(pfw);

}

 

intmain(intargc,char *argv[])

{

   char *path1 = "G:\\1.txt";

   char *path2 = "G:\\2.txt";

   char *path3 = "G:\\3.txt";

 

   //jia(path1, path2);

   jie(path2,path3);

 

   system("pause");

   return 0;

}

 

 

 

密码加密

头文件:

#define_CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

//字符串加密

char *stringjiami(char *password,char *string);

//字符串解密

char *stringjiemi(char *password,char *jiastring);

voidfilejiami(char *path,char *pathjia,char *password);

voidfilejiemi(char *pathjia,char *pathjie,char *password);

 

 

 

 

 

#include"encrytiondecrytion.h"

 

intgetfilesize(char *path)

{

   FILE *pf = fopen(path,"r");

   if (pf == NULL)

   {

       return -1;

   }

   else

   {

       fseek(pf, 0, SEEK_END);//文件末尾

       intlength =ftell(pf);

       returnlength;//返回长度

   }

}

 

char *stringjiami(char *password,char *string)

{

   //获取加密长度

   intpasslength =strlen(password);

   //获取字符串长度

   intstringlength =strlen(string);

   if (stringlength % passlength == 0)

   {

       //循环次数

       intci =stringlength /passlength;

       inti,j;

       for (i = 0; i <ci;i++)  //循环次数

       {

           //循环密码

           for (j = 0; j <passlength;j++)

           {

               string[passlength*i + j] ^=password[j];

           }

       }

   }

   else

   {

       intci =stringlength /passlength;//循环次数

       inti,j;

       for (inti = 0;i <ci;i++)

       {

           //循环密码

           for (j = 0; j <passlength;j++)

           {

               string[passlength * i +j] ^=password[j];

           }

       }

       intlastlength =stringlength %passlength;//剩下的长度

       for (inti = 0;i <lastlength;i++)

       {

           string[passlength*(stringlength / passlength) +i] ^=password[i];

       }

   }

   returnstring;

}

 

//字符串解密

char *stringjiemi(char *password,char *jiastring)

{

   //获取加密长度

   intpasslength =strlen(password);

   //获取字符串长度

   intstringlength =strlen(jiastring);

   if (stringlength %passlength == 0)

   {

       intci =stringlength /passlength;//循环次数

       inti,j;

       for (i = 0; i <ci;i++)

       {

           for (j = 0; j <passlength;j++)

           {

               //异或加密

               jiastring[passlength * i +j] ^=password[j];

           }

       }

   }

   else

   {

       //循环次数

       intci =stringlength /passlength;

       inti,j;

       for (i = 0; i <ci;i++)

       {

           //循环密码

           for (j = 0; j <passlength;j++)

           {

               //异或加密

               jiastring[passlength*i + j] ^=password[j];

           }

       }

       //剩下的长度

       intlastlength =stringlength %passlength;

       for (inti = 0;j <lastlength;i++)

       {

           jiastring[passlength*(stringlength / passlength) +i] ^=password[i];

       }

   }

   returnjiastring;

}

 

voidfilejiami(char *path,char *pathjia,char *password)

{

   FILE *pfr, *pfw;

   pfr =fopen(path,"r");

   pfw =fopen(pathjia,"w");

   if (pfr == NULL ||pfw ==NULL)

   {

       fclose(pfr);

       fclose(pfw);

       return;

   }

   else

   {

       intlength =getfilesize(path);

       char *newstr = (char*)malloc(sizeof(char)*(length + 1));

       for (inti = 0;i <length;i++)

       {

           charch =fgetc(pfr); //获取一个字符

           newstr[i] = ch;//不断存入字符

       }

       //字符串处理为'\0'

       newstr[length] = '\0';

       //加密字符串

       stringjiami(password,newstr);

 

       inti;

       for (i = 0; i <length;i++)

       {

           //挨个写入字符

           fputc(newstr[i],pfw);

       }

   }

   fclose(pfr);

   //关闭文件

   fclose(pfw);

}

 

main.c

#define_CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

#include"encrytiondecrytion.h"

 

intmain(intargc,char *argv[])

{

   charstring[50] ="锄禾日当午";

   char *password = "123";

   printf("%s\n",stringjiami(password,string));

   printf("%s\n",stringjiami(password,string));

 

   char *path1 = "G:\\1.txt";

   char *path2 = "G:\\2.txt";

   char *path3 = "G:\\3.txt";

 

   filejiami(path1,path2,"ABCDE");

   filejiami(path2,path3,"ABCDE");

 

   system("pause");

}

 

fscanf

#define_CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

 

intmain(intargc,char *argv[])

{

   charstr[100] = { 0 };

   fscanf(stdin,"%s",str);

   fprintf(stdout,"str=%s\n",str);

 

   system(str);

   return 0;

}

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击