日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

文件加解密,文件操作

發布時間:2024/9/27 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件加解密,文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1、fseek,ftell,fread,fwrite(簡單文件加密)

#define _CRT_SECURE_NO_WARNINGS? //去掉安全檢查

#include <stdio.h>

#include <stdlib.h>

?

/*讀取文件大小*/

int getfilesize(char *path)

{

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

??? if (pf == NULL)

??? {

??????? fclose(pf);

??????? return -1;

??? }

??? else

??? {

??????? fseek(pf,0,SEEK_END);

??????? int length = ftell(pf);

??????? //獲取文件大小

??????? return length;

??? }

??? fclose(pf);

}

?

/*實現文件復制*/

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;

??? }

??? else

??? {

??????? int length = 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);

??? }

}

?

void encryptyfile(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

??? {

??????? int length = getfilesize(oldpath);

??????? //分配內存,讀取文件

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

??????? //讀取二進制到內存

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

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

??????? {

??????????? //加密方法是,與制定字符進行異或操作

??????????? p[i] ^= 'A';

??????? }

??????? //寫入二進制到文件

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

?

??????? //關閉文件

??????? fclose(pfr);

??????? fclose(pfw);

??? }

}

?

/*解密文件*/

void decodefile(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

??? {

??????? int length = getfilesize(oldpath);

??????? //分配內存,讀取文件

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

??????? //讀取二進制到內存

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

??????? int i;

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

??? ??? {

??????????? p[i] ^= 'A';

??? ??? }

??????? //寫入二進制到文件

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

??? }

??? //關閉文件

??? fclose(pfr);

??? fclose(pfw);

}

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

}

上面的過程將會把加解密的文件輸出到文件中。

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

?

char jiami(char ch)

{

??? return ch ^ 123;

}

?

char jiemi(char ch)

{

??? return ch ^ 123;

}

?

void jia(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))

??????? {

??????????? //讀取字符

??????????? char ch = fgetc(pfr);

??????????? putchar(ch);

??????????? //輸出字符的時候將字符加密

??????????? fputc(jiami(ch), pfw);

??????? }

??? }

??? fclose(pfr);

??? fclose(pfw);//關閉文件

}

?

/*解密*/

void jie(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);

}

?

int main(int argc,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);

void filejiami(char *path, char *pathjia, char *password);

void filejiemi(char *pathjia, char *pathjie, char *password);

?

?

?

?

?

#include "encrytiondecrytion.h"

?

int getfilesize(char *path)

{

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

??? if (pf == NULL)

??? {

??????? return -1;

??? }

??? else

??? {

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

??????? int length = ftell(pf);

??????? return length;//返回長度

??? }

}

?

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

{

??? //獲取加密長度

??? int passlength = strlen(password);

??? //獲取字符串長度

??? int stringlength = strlen(string);

??? if (stringlength % passlength == 0)

??? {

??????? //循環次數

??????? int ci = stringlength / passlength;

??????? int i,j;

??????? for (i = 0; i < ci;i++)?? //循環次數

??????? {

??????????? //循環密碼

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

??????????? {

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

??????????? }

??????? }

??? }

??? else

??? {

??????? int ci = stringlength / passlength; //循環次數

??????? int i, j;

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

??????? {

??????????? //循環密碼

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

??????????? {

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

??????????? }

??????? }

??????? int lastlength = stringlength % passlength;//剩下的長度

??????? for (int i = 0; i < lastlength;i++)

??????? {

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

??????? }

??? }

??? return string;

}

?

//字符串解密

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

{

??? //獲取加密長度

??? int passlength = strlen(password);

??? //獲取字符串長度

??? int stringlength = strlen(jiastring);

??? if (stringlength %passlength == 0)

??? {

??????? int ci = stringlength / passlength;//循環次數

??????? int i, j;

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

??????? {

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

??????????? {

??????????? ??? //異或加密

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

??????????? }

??????? }

??? }

??? else

??? {

??????? //循環次數

??????? int ci = stringlength / passlength;

??????? int i, j;

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

??????? {

??????????? //循環密碼

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

??????????? {

??????????????? //異或加密

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

??????????? }

??????? }

??????? //剩下的長度

??????? int lastlength = stringlength % passlength;

??????? for (int i = 0; j < lastlength;i++)

??????? {

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

??????? }

??? }

??? return jiastring;

}

?

void filejiami(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

??? {

??????? int length = getfilesize(path);

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

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

??????? {

??????????? char ch = fgetc(pfr);? //獲取一個字符

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

??????? }

??????? //字符串處理為'\0'

??????? newstr[length] = '\0';

??????? //加密字符串

??????? stringjiami(password, newstr);

?

??????? int i;

??????? 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"

?

int main(int argc,char *argv[])

{

??? char string[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>

?

int main(int argc, char *argv[])

{

??? char str[100] = { 0 };

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

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

?

??? system(str);

??? return 0;

}

?

?

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的文件加解密,文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。