C 之 文件加解密

来源:互联网 发布:医疗器械销售公司知乎 编辑:程序博客网 时间:2024/05/30 04:47
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>//文件加密void crpypt(char normal_path[],char crypt_path[],char pwd[]){//打开文件FILE *normal_fp = fopen(normal_path, "r");FILE *crypt_fp = fopen(crypt_path, "w");//一次读取一个字节int ch;//循环使用密码中的字母进行异或运算int i = 0;//密码长度int pwd_len = strlen(pwd);while ((ch = fgetc(normal_fp)) != EOF){fputc(ch^pwd[i%pwd_len], crypt_fp);i++;}//关闭fclose(normal_fp);fclose(crypt_fp);}//解密void decrpypt(char crypt_path[], char decrypt_path[], char pwd[]){//打开文件FILE *normal_fp = fopen(crypt_path, "r");FILE *crypt_fp = fopen(decrypt_path, "w");//一次读取一个字符int ch;//循环使用字母中的密码进行异或运算int i = 0;//密码长度int pwd_len = strlen(pwd);while ((ch=fgetc(normal_fp))!=EOF){//写入fputc(ch^pwd[i%pwd_len],crypt_fp);i++;}//关闭fclose(crypt_fp);fclose(normal_fp);}void main(){/*char * readPath = "D:\\qq.png";FILE *fp = fopen(readPath, "r");//重新定位文件指针fseek(fp, 0, SEEK_END);//返回当前文件指针,相对于文件开头的位移量long fileSize = ftell(fp);printf("文件大小:%d\n", fileSize);*/char* normal_path = "D:\\test.txt";char* crypt_path = "D:\\crypttest.txt";char* decrypt_path = "D:\\crypttest2.txt";//加密//crpypt(normal_path, crypt_path, "hello world");//解密decrpypt(crypt_path, decrypt_path,"hello world");getchar();}