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!

发布了108 篇原创文章 · 获赞 114 · 访问量 8564

猜你喜欢

转载自blog.csdn.net/weixin_45773503/article/details/104693073