天天看點

檔案加解密,檔案操作



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);

上一篇: 鍊式棧
下一篇: 連結清單

繼續閱讀