C++实现凯撒密码加解密

密钥为3

//凯撒密码,密钥为3
#include 
using namespace std;
class code {
public:
	char a[100],b[100];
	void crypto();
	void decrypto();
};
void code::crypto() 
{
	cout << "请输入明文:" << endl;
	cin >> a;
	cout << "密文是:"<= 'x' && a[i] <= 'z'||a[i] >= 'X' && a[i] <= 'Z')
			b[i] = a[i] - 23;
		else 
			b[i] = a[i] + 3; 
	cout <> a;
	cout << "明文是:"<= 'a' && a[i] <= 'c' || a[i] >= 'A' && a[i] <= 'C')
			b[i] = a[i] + 23;
		else
			b[i] = a[i] - 3;
	cout << b[i];
	}	
	cout << endl;
}
int main()
{
	int m=1;
	code c;
	while (m)
	{
		cout << "请选择模式:1加密、2解密、3退出" << endl;
		cin >> m;
		if (m == 1) c.crypto();
		else if (m == 2) c.decrypto();
		else if (m == 3) break;
		else  cout << "您的输入有问题" << endl;
	}
	return 0;
}

密钥为1-26任意数字

//凯撒密码
#include 
using namespace std;
class code {
public:
	char a[100], b[100], n;
	void crypto(int n);
	void decrypto(int n);
};
void code::crypto(int n)
{
	cout << "请输入明文:" << endl;
	cin >> a;
	cout << "密文是:" << endl;
	for (int i = 0;a[i] != 0;i++)
	{
		if (a[i] >= 'a' && a[i] <= 'z')
			b[i] = (a[i] - 'a' + n) % 26 + 'a';
		else if (a[i] >= 'A' && a[i] <= 'Z')
			b[i] = (a[i] - 'A' + n) % 26 + 'A';
		cout << b[i];
	}
	cout << endl;
}
void code::decrypto(int n)
{
	cout << "请输入密文:" << endl;
	cin >> a;
	cout << "明文是:" << endl;
	for (int i = 0;a[i] != 0;i++)
	{
		if (a[i] >= 'a' && a[i] <= 'z') 
			b[i] = (a[i] - 'z' - n) % 26 + 'z';
		else if (a[i] >= 'A' && a[i] <= 'Z')
			b[i] = (a[i] - 'Z' - n) % 26 + 'Z';
		cout << b[i];
	}
	cout << endl;
}
int main()
{
	int m = 1,n;
	code c;
	while (m)
	{
		cout << "请选择模式:1加密、2解密、3退出" << endl;
		cin >> m;
		if (m == 1 || m == 2)
		{
			cout << "请输入密钥" << endl;
			cin >> n;
			if (n>=1&&n<=26)
			{
				if (m == 1) c.crypto(n);
				else if (m == 2) c.decrypto(n);
			}
			else {
				cout << "您的输入有问题" << endl; break;
			}
		}
		else if (m == 3) break;
		else  cout << "您的输入有问题" << endl;
	}
	return 0;
}

C++实现凯撒密码加解密_第1张图片

你可能感兴趣的:(C++,c++)