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

📄 004 b.cpp

📁 加密解密程序,把明玛与密匙进行一定的操作得到密码
💻 CPP
字号:
// 004 b.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
#include <string>
using namespace std;

//代换密码加密
void C(char a[],int n)
{
	string s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string s2 = "abcdefghijklmnopqrstuvwxyz";
	string s3,k;
	cout<<"请输入大写字母的密钥:"<<endl;
	char ch;
	while((ch=cin.get())!='\n')
		k+=ch;
	cout<<"加密前明文为:";
	for(int p=0;p<n;p++)
		cout<<a[p];
	cout<<endl;
	cout<<"加密后密文为:";
	for(int i=0;i<k.length();i++)
	{
		for(int j=0;j<26;j++)
		{
			if(k[i]==s1[j])
			{
				s3+=k[i];
				s1.erase(j,1);	
			}
		}
	}
	s3.append(s1);
	for(int l=0;l<n;l++)
	{
		for(int h=0;h<26;h++)
			if(a[l]==s2[h])
				a[l]=s3[h];
			cout<<a[l];
	}
	cout<<endl;
}
//代换密码解密
void D(char a[],int n)
{
	string s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string s2 = "abcdefghijklmnopqrstuvwxyz";
	string s3,k;
	cout<<"请输入大写字母的密钥:"<<endl;
	char ch;
	while((ch=cin.get())!='\n')
		k+=ch;		
	cout<<"解密前密文为:";
	for(int p=0;p<n;p++)
		cout<<a[p];
	cout<<endl;
	cout<<"解密后明文为:";
	for(int i=0;i<k.length();i++)
	{
		for(int j=0;j<26;j++)
		{
			if(k[i]==s1[j])
			{
				s3+=k[i];
				s1.erase(j,1);				
			}
		}
	}
	s3.append(s1);
	for(int l=0;l<n;l++)
	{
		for(int h=0;h<26;h++)
			if(a[l]==s3[h])
				a[l]=s2[h];
			cout<<a[l];
	}
	cout<<endl;
}

void main()
{
	int sign=1;
	int m;
	while(sign==1)
	{
		cout<<"代换密码加密----1,代换密码解密----2,退出----0 :"<<endl;
		cin>>m;
		char s[100];
		switch(m)
		{
			case 0: sign=0;
					break;
			case 1: cout<<"请输入要加密的小写字母字符串:"<<endl;
					cin>>s;
					cin.get();
					C(s,strlen(s));
					break;
			case 2: cout<<"请输入要加密的大写字母字符串:"<<endl;
					cin>>s;
					cin.get();
					D(s,strlen(s));
					break;
			default: cout<<"Input error!"<<endl;
		}
	}	
}

⌨️ 快捷键说明

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