用字符串对文本加密
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> void jiamis(char *password,int length); void jiemis(char *password, int length); int main(void){ char *password = "321"; int length = strlen(password); jiamis(password,length); jiemis(password,length); return 0; } void jiamis(char *password, int length){ FILE *fpr, *fpw; char *filenamer = "E:\\file\\1.txt"; char *filenamew = "E:\\file\\jiami.txt"; char ch; int i = 0; fpr = fopen(filenamer, "r"); fpw = fopen(filenamew, "w"); if (fpr == NULL || fpw == NULL){ printf("加密失败\n"); return; } //循环读取文件 while (1){ ch = fgetc(fpr); if (feof(fpr)) break; ch += password[i++%length]; fputc(ch, fpw); } fclose(fpr); fclose(fpw); } void jiemis(char *password, int length){ FILE *fpr, *fpw; char *filenamer = "E:\\file\\jiami.txt"; char *filenamew = "E:\\file\\2.txt"; char ch; int i = 0; fpr = fopen(filenamer, "r"); fpw = fopen(filenamew, "w"); if (fpr == NULL || fpw == NULL){ printf("解密失败\n"); return; } while (1){ //读取出来 ch = fgetc(fpr); if (feof(fpr))break; //解密 ch -= password[i++%length]; fputc(ch, fpw); } fclose(fpr); fclose(fpw); }
这是一个程序加密的程序。用字符串加密的,但是如果文本内有很多汉字的话,为什么不要加密