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

📄 zhihuan.cpp

📁 置换密码算法的原理是不改变明文字符
💻 CPP
字号:
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string ah;
int *Q;
bool GetKey()      //密钥的处理
{
    int i;
	cout<<"密钥得到并处理:"<<endl;
	cout<<"    请输入密钥(密钥为随机排序的26个字母),输入以回车结束:";
	cin>>ah;
	for(i=0;i<ah.length()*2;i++)
	{
		if(ah[i]<='9'&&ah[i]>='0')
		{
			return false;
		}
	}
	Q=new int[ah.length()*2];
	for(i=0;i<ah.length()*2;i++)
	{
		Q[i]=0;
	}
	for( i=0;i<ah.length();i++)
	{
	
		Q[i]=0;
		for(int j=0;j<ah.length();j++)
		{
			if(ah[i]>ah[j])
				Q[i]++;
		}
	}
	for(i=0;i<ah.length();i++)
	{
		Q[i]++;
	}
	cout<<"    通过密钥得到的序列为:";
	for(i=0;i<ah.length();i++)
	cout<<Q[i]<<" ";
	cout<<endl;
	cout<<endl;
	return true;}


void jiami()          //明文加密操作
{
	char o_word[100];
	int m=0,x=0;
	cout<<"进行明文加密:"<<endl;
	cout<<"    请输入要加密的明文:";
	gets(o_word);
	m=strlen(o_word);

	for(x=0;x<(ah.length()-m%ah.length())%ah.length();x++)
	{
		o_word[m+x]=' ';
	}
	o_word[m+x]='\0';
	for(int n=0;n<m;n=n+ah.length())
	{
		int temp=0;
		char b[100];
		for(int p=0;p<ah.length();p++)
		{
			b[p]=o_word[n+p];
		}
		for(int q=0;q<ah.length();q++)
		{
			o_word[n+q]=b[Q[ah.length()+q]-1];
		}
	}		
	cout<<"得到的密文为:"<<o_word<<endl;
	cout<<endl;
}


void jiemi()          //明文加密操作
{
	char s_word[100];
	int m=0,x=0;  
	cout<<"进行密文解密:"<<endl;
	cout<<"    请输入要解密的密文:";
	gets(s_word);
	m=strlen(s_word);
	for(x=0;x<(ah.length()-m%ah.length())%ah.length();x++)

	{
		s_word[m+x]=' ';
	}
	s_word[m+x]='\0';
	for(int n=0;n<m;n=n+ah.length())
	{
		int temp=0;
		char b[100];
		for(int p=0;p<ah.length();p++)
		{
			b[Q[ah.length()+p]-1]=s_word[n+p];
		}
		for(int q=0;q<ah.length();q++)
		{		
			s_word[n+q]=b[q];
		}
	}
	cout<<"得到的明文为:"<<s_word<<endl;
	cout<<endl;
}


int main()
{
	char t;
	cout<<"           ****************欢迎使用置换密码功能*******************"<<endl;
    cout<<endl;
	while(!GetKey())
	{
		cout<<"请输入正确的密钥!!"<<endl;
		cout<<endl;
	}

	for(int m=0;m<ah.length();m++)
	{
		Q[m+ah.length()]=Q[m];
		Q[m]=m+1;
	}
	cout<<"请选择(1)加密或(2)解密或(3)退出:";
	fflush(stdin);
	while(scanf("%c",&t)>0)
	{
		fflush(stdin);
		switch(t)
		{
		case '1': jiami();break;
		case '2': jiemi();break;
		case '3': cout<<"程序结束!"<<endl;delete Q;return 0;
		default: break;
		}
		cout<<"请继续选择(1)加密或(2)解密或(3)退出:";
	}
	delete Q;
	return 0;
}

⌨️ 快捷键说明

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