- 文本文件读取是基于内容的读取而二进制文件读取是基于位置的读取,所以文本文件读取要加一个eof判断.而二进制文件读取则不用
- 文本文件加密
1 //文本加密 2 void wenben_jiami(char *path, char *desPath,char *mi) 3 { 4 FILE *pfr = fopen(path, "r"); 5 FILE *pfw = fopen(desPath, "w"); 6 int file_lengthr = getLength(path); 7 int mi_length = strlen(mi); 8 //亦或操作防止溢出 所以采用int类型 否则解密过程中可能读取到-1,从而造成丢失 9 int ch; 10 //能整除部分 11 for (int i = 0; i < file_lengthr / mi_length; i++) 12 { 13 for (int j = 0; j < mi_length; j++) 14 { 15 ch = fgetc(pfr); 16 17 //因为文本读取会多读取一位,所以要加以判断是否到结尾了 18 if (ch != EOF) 19 { 20 ch = ch ^ mi[j]; 21 fputc(ch, pfw); 22 } 23 } 24 } 25 26 //余数部分 27 for (int i = 0; i < file_lengthr%mi_length; i++) 28 { 29 ch = fgetc(pfr); 30 31 if (ch != EOF) 32 { 33 ch = ch ^ mi[i]; 34 fputc(ch, pfw); 35 } 36 } 37 38 fclose(pfr); 39 fclose(pfw); 40 }
- 二进制文件加密
1 //二进制文本加密 2 void erjinzhi_jiami(char *path, char *desPath, char *mi) 3 { 4 FILE *pfr = fopen(path, "rb"); 5 FILE *pfw = fopen(desPath, "wb"); 6 int file_lengthr = getLength(path); 7 int mi_length = strlen(mi); 8 char ch; 9 10 //能整除部分 11 for (int i = 0; i < file_lengthr / mi_length; i++) 12 { 13 for (int j = 0; j < mi_length; j++) 14 { 15 ch = fgetc(pfr); 16 ch = ch ^ mi[j]; 17 fputc(ch, pfw); 18 } 19 } 20 21 //余数部分 22 for (int i = 0; i < file_lengthr%mi_length; i++) 23 { 24 ch = fgetc(pfr); 25 ch = ch ^ mi[i]; 26 fputc(ch, pfw); 27 } 28 29 fclose(pfr); 30 fclose(pfw); 31 }
完整代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #define srcPath "1.txt" 7 #define jiaPath "加密后的数据.txt" 8 #define jiePath "解密后的数据.txt" 9 10 int getLength(char *path) 11 { 12 FILE *pf = fopen(srcPath,"r"); 13 if (pf) 14 { 15 fseek(pf, 0, SEEK_END); 16 int length = ftell(pf); 17 return length; 18 } 19 return 0; 20 } 21 22 //文本加密 23 void wenben_jiami(char *path, char *desPath,char *mi) 24 { 25 FILE *pfr = fopen(path, "r"); 26 FILE *pfw = fopen(desPath, "w"); 27 int file_lengthr = getLength(path); 28 int mi_length = strlen(mi); 29 //亦或操作防止溢出 所以采用int类型 否则解密过程中可能读取到-1,从而造成丢失 30 int ch; 31 //能整除部分 32 for (int i = 0; i < file_lengthr / mi_length; i++) 33 { 34 for (int j = 0; j < mi_length; j++) 35 { 36 ch = fgetc(pfr); 37 38 //因为文本读取会多读取一位,所以要加以判断是否到结尾了 39 if (ch != EOF) 40 { 41 ch = ch ^ mi[j]; 42 fputc(ch, pfw); 43 } 44 } 45 } 46 47 //余数部分 48 for (int i = 0; i < file_lengthr%mi_length; i++) 49 { 50 ch = fgetc(pfr); 51 52 if (ch != EOF) 53 { 54 ch = ch ^ mi[i]; 55 fputc(ch, pfw); 56 } 57 } 58 59 fclose(pfr); 60 fclose(pfw); 61 } 62 63 //二进制文本加密 64 void erjinzhi_jiami(char *path, char *desPath, char *mi) 65 { 66 FILE *pfr = fopen(path, "rb"); 67 FILE *pfw = fopen(desPath, "wb"); 68 int file_lengthr = getLength(path); 69 int mi_length = strlen(mi); 70 char ch; 71 72 //能整除部分 73 for (int i = 0; i < file_lengthr / mi_length; i++) 74 { 75 for (int j = 0; j < mi_length; j++) 76 { 77 ch = fgetc(pfr); 78 ch = ch ^ mi[j]; 79 fputc(ch, pfw); 80 } 81 } 82 83 //余数部分 84 for (int i = 0; i < file_lengthr%mi_length; i++) 85 { 86 ch = fgetc(pfr); 87 ch = ch ^ mi[i]; 88 fputc(ch, pfw); 89 } 90 91 fclose(pfr); 92 fclose(pfw); 93 } 94 95 96 void main() 97 { 98 printf("%d", getLength(srcPath)); 99 erjinzhi_jiami(srcPath, jiaPath, "0123"); 100 erjinzhi_jiami(jiaPath, jiePath, "0123"); 101 system("pause"); 102 }