转轮密码机(C++实现加密)

#include 
#include 
using namespace std;
//快中慢轮子的转动次数
int count1 = 0;
int count2 = 0;
int count3 = 0;
class Wheel {
public:
	int right[26];
	int left[26];
	void trun();	//转动函数
	Wheel(int a[], int b[]) {
		for (int i = 0; i < 26; i++){
			right[i] = a[i];
			left[i] = b[i];
		}
	}
	//获取左表对应右表的坐标
	int getIndex(int x);
};
//转动轮子(一次一次的转动)
void Wheel::trun() {
	int temp1 = left[25],temp2 = right[25];
	for (int i = 25; i >= 1; i--){
		int j = i - 1;
		left[i] = left[j];
		right[i] = right[j];
	}
	left[0] = temp1;right[0] = temp2;
}
//这里传入的参数为左表的数值下标,返回值为右表下标
int Wheel::getIndex(int x) {
	//cout << right[x] << endl;
	int index;
	for (int i = 0; i < 26; i++){
		if (left[i] == right[x]) {
			index = i;
		}
	}
	//cout << index << endl;
	return index;
}
int main() {
	string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int a[] = { 24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 };
	int b[] = { 21,3,15,1,19,10,14,26,20,8,16,7,22,4,11,5,17,9,12,23,18,2,25,6,24,13 };
	int a1[] = { 26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25 };
	int b1[] = { 20,1,6,4,15,3,14,12,23,5,16,2,22,19,11,18,25,24,13,7,10,8,21,9,26,17 };
	int a2[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 };
	int b2[] = { 8,18,26,17,20,22,10,3,13,11,4,23,5,24,9,12,25,16,19,6,15,21,2,7,1,14 };
	//初始化三个轮子(慢,中,快)
	Wheel wheel1(a,b),wheel2(a1,b1),wheel3(a2,b2);
	string temp;
	getline(cin, temp);
	int index;
	int t;
	for (int i = 0; i < temp.length(); i++)
	{
		t = 3;
		char c = temp[i];
		if (c == ' ')
		{
			cout << " ";
		}
		else {
			//先获取下标(第一个轮子的左表下标)
			index = letters.find(c);
			//cout << index << endl;
			//获取最终的下标
			index = wheel1.getIndex(index);
			index = wheel2.getIndex(index);
			index = wheel3.getIndex(index);
			cout << letters[index];
			count1++;
			wheel3.trun();
			if (count1 == 26) {
				count1 = 0;
				count2++;
				wheel2.trun();
				if (count2 == 26) {
					count2 = 0;
					count3++;
					wheel1.trun();
					if (count3 == 26) {
						count3 = 0;
						count2 = 0;
						count1 = 0;
					}
				}
			}
		}
	}
	return 0;
}

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