转轮密码机的加解密算法实现

密码学课程的第一个实验,算法实现转轮机的加解密,此处用c++
转轮密码机的原理如图:
图一为初始状态,图二为输入一位明文后的状态
在这里插入图片描述
这是三转轮密码机,有慢轮、中轮、快轮三个速度不同的轮子,加密时:输出一位后,快轮逆时针转动一次,快轮转满26次即一圈时,中轮子转动一次,中轮转满26圈时,快轮转动一次。解密过程则反过来转。
具体注意点在源码注释中强调。

源码如下:

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

void TurnWheel(int* wheel_l,int* wheel_r);//加密时正向转动一次
void ReverseWheel(int* wheel_l,int* wheel_r);//解密时逆向转动一次
void Encrypt(char* mingwen,char* miwen,int* slow_l,int* slow_r,int* middle_l,int* middle_r,int* fast_l,int* fast_r);//加密
void Decrypt(char* miwen,char* mingwen,int* slow_l,int* slow_r,int* middle_l,int*middle_r,int* fast_l,int* fast_r);//解密

int main()
{
	int a;
	 while(true){
	cout<<"Please choose the action you want:\n1.Encrypt\t2.Decrypt"<<endl;
	cin>>a;
	//慢中快三个轮子的初始状态
	int slow_l[26]={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 slow_r[26]={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 middle_l[26]={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 middle_r[26]={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 fast_l[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,26};
	int fast_r[26]={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};

	char* mingwen=new char[128];
	char* miwen=new char[128];
	memset(mingwen,0,128);//初始化
	memset(miwen,0,128);

	switch(a)
	{ 
	case 1:
		system("cls");
		cout<<"Please input the string you want to entrypt:"<<endl;
		cin>>mingwen;
		Encrypt(mingwen,miwen,slow_l,slow_r,middle_l,middle_r,fast_l,fast_r);
		cout<<"cipertext:"<<endl;
		for(unsigned int i=0;i<strlen(miwen);i++)
		{
			cout<<miwen[i];
		}
		cout<<endl;
		break;
	case 2:
		system("cls");
		cout<<"Please input the string you want to decrypt:"<<endl;
		cin>>miwen;
		Decrypt(miwen,mingwen,slow_l,slow_r,middle_l,middle_r,fast_l,fast_r);
		cout<<"cleartext:"<<endl;
		for(unsigned int i=0;i<strlen(mingwen);i++)
		{
			cout<<mingwen[i];
		}
		cout<<endl;
		break;
	default:
		system("cls");
		break;
	}
	}
	return 0;
}

void TurnWheel(int* wheel_l,int* wheel_r)
{
	int temp=wheel_l[25];
	for(int i=25;i>0;i--)
		wheel_l[i]=wheel_l[i-1];
	wheel_l[0]=temp;

	temp=wheel_r[25];
	for(int i=25;i>0;i--)
		wheel_r[i]=wheel_r[i-1];
	wheel_r[0]=temp;
}
void ReverseWheel(int* wheel_l,int* wheel_r)
{
	int temp=wheel_l[0];
	for(int i=0;i<25;i++)
		wheel_l[i]=wheel_l[i+1];
	wheel_l[25]=temp;

	temp=wheel_r[0];
	for(int i=0;i<25;i++)
		wheel_r[i]=wheel_r[i+1];
	wheel_r[25]=temp;
}
void Encrypt(char* mingwen,char* miwen,int* slow_l,int* slow_r,int* middle_l,int* middle_r,int* fast_l,int* fast_r)
{
	int count=0;//快轮转动次数
	for(unsigned int i=0;i<strlen(mingwen);i++)//加密
	{
		if('A'<=mingwen[i]&&mingwen[i]<='Z')
		{
			int current=mingwen[i]-'A';//确定位置
			current=slow_l[current];
			for(int j=0;j<26;j++)
			{
				if(current==slow_r[j])
				{
					current=middle_l[j];
					break;
				}
			}
			for(int j=0;j<26;j++)
			{
				if(current==middle_r[j])
				{
					current=fast_l[j];
					break;
				}
			}
			for(int j=0;j<26;j++)
			{
				if(current==fast_r[j])
				{
					current=j;
					break;
				}
			}
			miwen[i]=char(current+65); 
			count++;
			TurnWheel(fast_l,fast_r);//快轮转动一次
			if(count%26==0)//判断是否要转动
			{
				TurnWheel(middle_l,middle_r);
				if(count%(26*26)==0)
				{
					TurnWheel(slow_l,slow_r);
				}
			}
		}
	}
}
void Decrypt(char* miwen,char* mingwen,int* slow_l,int* slow_r,int* middle_l,int*middle_r,int* fast_l,int* fast_r)
{
	int count=0;
	for(unsigned int i=0;i<strlen(miwen);i++)//先让轮子转成加密后的状态
	{
		if('A'<=miwen[i]&&miwen[i]<='Z')
		{
			count++;
			TurnWheel(fast_l,fast_r);
		}
	}	
	if(count%26==0)
	{
		TurnWheel(middle_l,middle_r);
		if(count%(26*26)==0)
		{
			TurnWheel(slow_l,slow_r);
		}
	}
	
	for(int i=strlen(miwen)-1;i>=0;i--)//解密
	{
		
		if('A'<=miwen[i]&&miwen[i]<='Z')
		{
			ReverseWheel(fast_l,fast_r);//快轮先转回去一次再解密

			int current=miwen[i]-'A';
			current=fast_r[current];
			for(int j=0;j<26;j++)
			{
				if(current==fast_l[j])
				{
					current=middle_r[j];
					break;
				}
			}
			for(int j=0;j<26;j++)
			{
				if(current==middle_l[j])
				{
					current=slow_r[j];
					break;
				}
			}
			for(int j=0;j<26;j++)
			{
				if(current==slow_l[j])
				{
					current=j;
					break;
				}
			}
				
			if(count%26==0)
			{
				ReverseWheel(middle_l,middle_r);
				if(count%(26*26)==0)
				{
					ReverseWheel(slow_l,slow_r);
				}
			}
			count--;//前面判断完了再减一
			mingwen[i]=char(current+65);
		}
	}
}


测试:
明文:HELLOWORLD
密文:MGPOTIMTJU

如发现问题欢迎一起交流讨论!俺也是第一次弄这个。。

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以回答这个问题。以下是一个简单的转轮密码加密和解密的 C 代码示例: ```c #include <stdio.h> #include <string.h> #define ROTORS 3 #define ALPHABET_SIZE 26 void rotate_rotors(int *rotors) { rotors[] = (rotors[] + 1) % ALPHABET_SIZE; if (rotors[] == ) { rotors[1] = (rotors[1] + 1) % ALPHABET_SIZE; if (rotors[1] == ) { rotors[2] = (rotors[2] + 1) % ALPHABET_SIZE; } } } char encrypt_char(char c, int *rotors, char **wiring) { int index = c - 'A'; for (int i = ; i < ROTORS; i++) { index = (index + rotors[i]) % ALPHABET_SIZE; index = wiring[i][index] - 'A'; } rotate_rotors(rotors); return index + 'A'; } char decrypt_char(char c, int *rotors, char **wiring) { int index = c - 'A'; for (int i = ROTORS - 1; i >= ; i--) { index = wiring[i][index] - 'A'; index = (index - rotors[i] + ALPHABET_SIZE) % ALPHABET_SIZE; } rotate_rotors(rotors); return index + 'A'; } void encrypt(char *plaintext, char *ciphertext, int *rotors, char **wiring) { int len = strlen(plaintext); for (int i = ; i < len; i++) { ciphertext[i] = encrypt_char(plaintext[i], rotors, wiring); } ciphertext[len] = '\'; } void decrypt(char *ciphertext, char *plaintext, int *rotors, char **wiring) { int len = strlen(ciphertext); for (int i = ; i < len; i++) { plaintext[i] = decrypt_char(ciphertext[i], rotors, wiring); } plaintext[len] = '\'; } int main() { char *wiring[ROTORS] = { "BDFHJLCPRTXVZNYEIWGAKMUSQO", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "EKMFLGDQVZNTOWYHXUSPAIBRCJ" }; int rotors[ROTORS] = {, , }; char plaintext[] = "HELLO"; char ciphertext[strlen(plaintext) + 1]; encrypt(plaintext, ciphertext, rotors, wiring); printf("Ciphertext: %s\n", ciphertext); char decrypted[strlen(ciphertext) + 1]; decrypt(ciphertext, decrypted, rotors, wiring); printf("Decrypted: %s\n", decrypted); return ; } ``` 这个代码实现了一个简单的转轮密码,使用了三个转轮和三个固定的接线板。`encrypt` 函数将明文加密为密文,`decrypt` 函数将密文解密为明文。`rotate_rotors` 函数将转轮向前旋转一个位置。`encrypt_char` 和 `decrypt_char` 函数分别加密和解密一个字符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值