昨天和一个朋友又看了一遍模拟游戏,晚上回到家里闲的无聊,就写了一个简单的enigma模拟器。主要熟悉了他的加密算法以及解密设置。总的来说并不复杂,今天早上还做了一下UI,虽然没有做反射器,但是觉得基本上也是一个挺好玩的东西了,所以贴出来~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ENIGMA
{
public class EnigmaSystem
{
//定义转子
public int[,] rotatorsPos;
public int[,] currentPos;
public Dictionary<char, int> input;
public Dictionary<int, char> output;
//构造器
public EnigmaSystem()
{
}
//初始化转子
public void Load(string path)
{
//初始化绑定输入输出字典
#region 初始化绑定
input =new Dictionary<char,int>(26);
for (int i= 65; i<=90; i++)
{
input.Add((char)i, i-64);
}
output = new Dictionary<int, char>(26);
for (int i = 65; i <= 90; i++)
{
output.Add( i - 64,(char)i);
}
#endregion
//初始化转子
rotatorsPos = new int [3,26]; //3*26
currentPos = new int [3, 26];
//从外部txt读入rotators固化线路的位置信息矩阵
TxtToRotators(path);
}
//设置转子
public void Setting(int rotator1=0, int rotator2=0, int rotator3=0)
{
int setting = rotator1 + 26 * rotator2 + 26 * 26 * rotator3;
currentPos = TurningRotator(setting);
}
//将Rotators数据写入到txt文件中
public void RotatorsToTxt(string path ="D:\\RotatorsData.txt" )
{
StreamWriter sw = new StreamWriter(path);
for (int i = 0; i < rotatorsPos.GetLength(0); i++)
{
for (int j = 1; j <= rotatorsPos.GetLength(1); j++)
{
sw.Write(rotatorsPos[i, j]);
sw.Write(' ');