C語言按行列加密解密(英文文本)

按行列加密解密

在這裏插入圖片描述
主函數:

#include <stdio.h>
void code(char *filename);
void dcode(char *filename);
int main(void)
{	
	code("public.txt");  /*加密*/
	//dcode("secret.txt"); /*解密*/
	return 0;	
} 

加密:

/*加密*/   /*函數功能化*/ 
void code(char *filename){
	char c, t, row=0, column=0;
	FILE *fp1,*fp2;
	fp1 = fopen(filename,"r");		/*以讀的方式打開*/ 
	if(fp1 == NULL){
		fp1 = fopen(filename,"w");	/*如果目錄裏沒有則以寫的方式打開*/ 
	}
	fp2 = fopen("secret.txt","w");	/*以覆蓋寫的方式打開*/ 
	do{
		c = fgetc(fp1);				/*從public文件中讀取字符*/ 
		t = c;						/*備份c*/ 
		if (t=='\r' || t=='\n')		/*如果遇到換行*/ 
		{
			column = 0; 			/*列爲0*/ 
			row++;					/*行++*/ 
		}
		else{
			column++;  				/*列++*/ 
			c = c+row+column;		/*加密*/ 
			/*以下三個條件減少密碼重複度*/ 
			if(row%2==0){
				c = c-1;     
			}
			if(row%3==0){
				c = c+1;     
			} 
			if(row%4==0){
				c = c-2;     
			}  
		}

		if(t!=EOF){					/*讀到文章結尾停止*/ 
		fprintf(fp2,"%c",c); 		/*將c寫入文件*/			
		}
	}while(t!=-1);
	fclose(fp1);
	fclose(fp2);
	remove(filename); 				/*刪除public文件*/
}

解密:

/*解密*/ 
void dcode(char *filename){
	char c, t, row=0, column=0;
	FILE *fp1,*fp2;
	fp1 = fopen(filename,"r");
	if(fp1 == NULL){
		fp1 = fopen(filename,"w");
	}
	fp2 = fopen("public.txt","w");
	do{
		c = fgetc(fp1);				/*從secret文件中讀取字符*/ 
		t = c;
		if (t=='\r' || t=='\n')
		{
			column = 0;
			row++;	
		}
		else{
			column++;
			c = c-row-column;   	/*解密*/
			if(row%2==0){
				c = c+1;     
			}
			if(row%3==0){
				c = c-1;     
			} 
			if(row%4==0){
				c = c+2;     
			}     
		}
		//printf("%c", c);     /*寫上可直接在運行框中顯示*/
		if(t!=EOF){
		fprintf(fp2,"%c",c);			
		}
	}while(t!=-1);
	fclose(fp1);
	fclose(fp2);
}

英語文本:
Good morning, ladies and gentlemen:
Some of us are having problems with our parents, as they often look into our school bags or read our diaries.
I fully understand why we are not comfortable about it, but there’s no need to feel too sad.
Our parents are checking our bags or diaries to make sure we are not getting into any trouble.
They have probably heard some horrible stories about other kids and thought we might do the same.
Or perhaps they just want to connect with us but are doing it all wrong .
My suggestion is: Tell them we want them to trust us as much as we’d like to trust them .
If you don’t think you can talk to them , write them a letter and leave it lying around —they are bound to read it .
Thank you!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章