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

📄 affinecipherbox.cpp

📁 280行的高效代码
💻 CPP
字号:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

using namespace std;

int const maxContain=100;

class AffineCipherBox
{
	char plainTEXT[maxContain];
	char cipherTEXT[maxContain];
	int Key[2];

public:
	void setplainTEXT(char *UserTypeIn)
	{
		strcpy(plainTEXT,UserTypeIn);
	}

	void setcipherTEXT(char *UserTypeIn)
	{
		strcpy(cipherTEXT,UserTypeIn);
	}

	void setKey(int *KeyAB)
	{
		for(int i=0;i<2;i++)
		{
			Key[i]=*(KeyAB+i);
		}
	}

	char *showplainTEXT()
	{
		return plainTEXT;
	}

	char *showcipherTEXT()
	{
		return cipherTEXT;
	}

/*	void showplaintext()
	{
		for(int j;plainTEXT[j];j++)
			cout<<plainTEXT[j];
	}

	void showciphertext()
	{
		for(int j;cipherTEXT[j];j++)
			cout<<cipherTEXT[j];
	}
*/
	int *showKey()
	{
		return Key;
	}

	void Encryption();

	void Decipher();
};

void AffineCipherBox::Encryption()
{
	int i;

	for(i=0;plainTEXT[i];i++)
	{
		if(isupper(plainTEXT[i]))
		{
			cipherTEXT[i]=(char)((Key[0]*(plainTEXT[i]-65)+Key[1])%26+65);
			cout<<cipherTEXT[i];
		}

		if(islower(plainTEXT[i]))
		{
			cipherTEXT[i]=(char)((Key[0]*(plainTEXT[i]-97)+Key[1])%26+97);
			cout<<cipherTEXT[i];
		}
		else
		{
			cipherTEXT[i]=plainTEXT[i];
			cout<<cipherTEXT[i];
		}
	}
}

void AffineCipherBox::Decipher()
{
	int i,j;
	int R_a;

	for(i=1;i<26;i++)
	{
		if((i*Key[0])%26==1)
		{
			R_a=i;
			break;
		}
	}
	for(j=0;cipherTEXT[j];j++)
	{
		if(isupper(cipherTEXT[j]))
		{
			plainTEXT[j]=(char)(((R_a*(cipherTEXT[j]-65-Key[1]))%26+26)%26+65);
			cout<<plainTEXT[j];
		}
		if(islower(cipherTEXT[j]))
		{
			plainTEXT[j]=(char)(((R_a*(cipherTEXT[j]-97-Key[1]))%26+26)%26+97);
			cout<<plainTEXT[j];
		}
		else
		{
			plainTEXT[j]=cipherTEXT[j];
			cout<<plainTEXT[j];
		}
	}
}

void draw()
{
	cout<<"/********************************************************************/\n";
	cout<<"/*                    This is a Affine Cipher box                   */\n";
	cout<<"/*                             P=C=Z26                              */\n";
	cout<<"/*                   K={(a,b)|a,b->Z26,gcd(a,26)=1}                 */\n";
	cout<<"/*                          e(a,b)(x)=ax+b                          */\n";
	cout<<"/*                         d(a,b)(y)=a*(y-b)                        */\n";
	cout<<"/********************************************************************/\n";
}

int Whattodo()
{
	int nu;
	
	do
	{
		cout<<"What do you want to do?...";
		cout<<"1.encryption;";
		cout<<"2.decipher.";

		cin>>nu;
		if((nu!=1)&&(nu!=2))
			cout<<"you entered a wrong number!\n";
	}while(nu!=2&&nu!=1);

	return nu;
}

void Userput(char *UserputIn,int Whattodo)
{
	if(Whattodo==1)
	{
		cout<<"please enter your plaintext:";
		cin>>UserputIn;
		//gets(UserputIn);
	}
	if(Whattodo==2)
	{
		cout<<"please enter your ciphertext:";
		cin>>UserputIn;
		//gets(UserputIn);
	}
}

int AutoRandomKey()
{
	int i;

	do
	{
		i=rand()%26;
	}while(!((i%2)&&(i%13)));

	return i;
}

void AutoDecipher(AffineCipherBox a)
{
	int KeyAcycle,KeyBcycle;
	int tempKey[2];
	char pause;

	for(KeyAcycle=1;KeyAcycle<26;KeyAcycle++)
	{
		if(pause=='d')
			break;
		if(KeyAcycle%2&&KeyAcycle%13)
		{
			for(KeyBcycle=0;KeyBcycle<26;KeyBcycle++)
			{
				tempKey[0]=KeyAcycle;
				tempKey[1]=KeyBcycle;
				a.setKey(tempKey);
				a.Decipher();
				cout<<"("<<tempKey[0]<<","<<tempKey[1]<<")\n";
			}
			cout<<"press c for continue.....";
			cout<<"press d for end decipher.";
			cin>>pause;	
		}
	}
}

int main()
{
	int saveWhattodo;
	char saveKeyflag;
	int savekey[2];
	char tempUserputIn[maxContain];
	AffineCipherBox one;

	draw();

	saveWhattodo=Whattodo();
	Userput(tempUserputIn,saveWhattodo);
	if(saveWhattodo==1)
		one.setplainTEXT(tempUserputIn);
	if(saveWhattodo==2)
		one.setcipherTEXT(tempUserputIn);

	cout<<"\nDo you want computer make a random key(y/n)? ";
	cin>>saveKeyflag;
	if(saveKeyflag=='y')
	{
		srand((unsigned)time(NULL));
		for(int i=0;i<2;i++)
		{
			savekey[0]=AutoRandomKey();
			savekey[1]=rand()%26;
		}
		one.setKey(savekey);
		cout<<"Your Key is :"<<savekey[0]<<","<<savekey[1]<<"\n";
	}
	if(saveKeyflag=='n')
	{
		do
		{
			cout<<"\na=:";
			cin>>savekey[0];
			cout<<"\nb=:";
			cin>>savekey[1];
		    if(!((savekey[0]%2)&&(savekey[0]%13)))
				cout<<"gcd(KeyA,26)=1";
		}while(!((savekey[0]%2)&&(savekey[0]%13)));
		one.setKey(savekey);
	}

	if(saveKeyflag=='A')
	{
		AutoDecipher(one);
		goto END;
	}

	if(saveWhattodo==1)
	{
		cout<<"\nthe cipherTEXT is :";
		one.Encryption();
	}
	if(saveWhattodo==2)
	{
		cout<<"\nthe plainTEXT is :";
		one.Decipher();
	}
END:
	cout<<"\n";

	//gets(tempUserputIn);

	//cout<<tempUserputIn;

	return 0;
}

⌨️ 快捷键说明

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