⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 palyfair.cpp

📁 本程序是基于playfair算法的一种验证性算法
💻 CPP
字号:
#include<stdio.h>
#include<string.h>
#include<process.h>
void  usage(int argc );
int   getx(char c);
int   gety(char c);
char box[5][5]={'p' , 'l' , 'a' , 'y' , 'f', 
'i' , 'r' , 'b' , 'c' , 'd',
'e' , 'g' , 'h' , 'k' , 'm',
'n' , 'o' , 'q' , 's' , 't',
'u' , 'v' , 'w' , 'x' , 'z'
};
void main(int argc , char *argv[])
{
	usage(argc);
	FILE *fpin,*fpout;
	char temp1,temp2,out1,out2;
	int firstx,firsty,secondx,secondy;
	if((fpin=fopen(argv[1],"rb"))==NULL)
	{
		printf("%s open failed!\n",argv[1]);
		exit(0);
	}
	if((fpout=fopen(argv[2],"wb"))==NULL)
	{
		printf("%s create failed!\n",argv[2]);
		exit(0);
	}
	while(temp1=fgetc(fpin),!feof(fpin))
	{
		while(temp1=fgetc(fpin),!feof(fpin))
		{
			temp2=fgetc(fpin);
			if(feof(fpin))
			{
				temp2='x';
				printf("x替代了结尾!\n");
			} 
		}
		if(temp1==temp2)
		{
			fseek(fpin,-1L,1);
			temp2='x';
		}
		if(temp1=='j')
			temp1='i';
		if(temp2=='j')
			temp2='i';
		firstx=getx(temp1);
		firsty=gety(temp1);
		secondx=getx(temp2);
		secondy=gety(temp2);
		if(firstx==secondx)
		{
			out1=box[firstx][((firsty+6)%5)];
			out2=box[firstx][((secondy+6)%5)];
		}
		else if(firsty==secondy)
		{
			out1=box[((firstx+1)%5)][firsty];
			out2=box[((secondx+1)%5)][firsty];
		}
		else
		{
			out1=box[firstx][secondy];
			out2=box[secondx][firsty];
		}
		fputc(out1,fpout);
		fputc(out2,fpout);
	}
	printf("ok!\n");
	fclose(fpin);
	fclose(fpout);
}
void usage(int argc )
{
	if(argc!=3)
	{
		printf("usage: playfair sourcefile destfile\n");
		exit(0);
	}
}
int getx(char c)
{
	int k=0;
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
			if(box[i][j]==c)
				k=i;
	}
	return k;
}
int gety(char c)
{
	int k=0;
	int i;
	int j;
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
			if(box[i][j]==c)
				k=j;
	}
	return k;
}

  
 


 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -