本文介绍了C#谜题,有错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!在我在互联网上遇到的某人的巨大帮助下,我得以用C#编写一个Enigma.但是只有三个转子和反射器.当我输入"TEST"时,"OLPF"出现.所有转子都设置在A-A-A上.代码如下:
Hey guys! With some great help from someone I met on the internet, I was able to programm an Enigma in C#. But only it''s three rotors and the reflector. When I type "TEST" , "OLPF" comes out. All rotors are set on A-A-A. Here''s the code:
namespace Enigma
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// in this part I declare the variables
// int declares a number, string a letter
int n = 0;
int i = 0;
int ii = 0;
int iii = 0;
int notch3 = 22; // if the first rotor reaches the 22. letter, the second rotor will turn one time
int notch2 = 5; // if the second rotor reaches the 5. letter, the third will turn one time
string alpha = "ABCDEFGHJIKLMNOPQRSTUVWXYZ";
wird sondern nur ABCDEFGH....Z
public static string rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"; // Rotor III
public static string rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; // Rotor II
public static string rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"; // Rotor I
public static string reflector1 = "YRUHQSLDPXNGOKMIEBFZCWVJAT"; // Reflektor
// with the numericUpDown elements, you can configurate the position of the three rotors. (in my Case 1-1-1)
private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
{
label1.Text = alpha.Substring((int)numericUpDown1.Value - 1, 1); //a
}
private void numericUpDown2_ValueChanged_1(object sender, EventArgs e)
{
label2.Text = alpha.Substring((int)numericUpDown2.Value - 1, 1);
}
private void numericUpDown3_ValueChanged_1(object sender, EventArgs e)
{
label3.Text = alpha.Substring((int)numericUpDown3.Value - 1, 1);
}
// tbxInput ist das Fenster, in dem man den Klartext eingibt. Der eingegebene Text
//wird durch den Algorithmus, den ich "Encrypt" genannt habe verschlüsselt.
private void tbxInput_TextChanged_1(object sender, EventArgs e)
{
Encrypt();
}
private void tbxOutput_TextChanged(object sender, EventArgs e)
{
}
// thats the main part. Encryp() contains the algorithm which encrypts the letters. also here should be the solution for my question but I don't get it....
private void Encrypt()
{
// declare variables
string sInput = tbxInput.Text.Substring(tbxInput.Text.Length - 1, 1);
string sOutput = sInput.ToUpper();
i = Int32.Parse(numericUpDown1.Value.ToString()) - 1;
ii = Int32.Parse(numericUpDown2.Value.ToString()) - 1;
iii = Int32.Parse(numericUpDown3.Value.ToString()) - 1;
if (sInput == " ")
{
sOutput = " ";
tbxOutput.Text += " ";
return;
}
Increment_Rotors();
n = alpha.IndexOf(sOutput) + iii;
validateN();
sOutput = rotor3.Substring(n, 1);
n = alpha.IndexOf(sOutput) + ii - iii;
validateN();
sOutput = rotor2.Substring(n, 1);
n = alpha.IndexOf(sOutput) + i - ii;
validateN();
sOutput = rotor1.Substring(n, 1);
n = alpha.IndexOf(sOutput);
validateN();
n = alpha.IndexOf(sOutput) - i;
validateN();
// here comes the reflector
sOutput = reflector1.Substring(n, 1);
n = alpha.IndexOf(sOutput) + i;
validateN();
sOutput = alpha.Substring(n, 1);
n = rotor1.IndexOf(sOutput) - i;
validateN();
sOutput = alpha.Substring(n, 1);
n = alpha.IndexOf(sOutput) + ii;
validateN();
sOutput = alpha.Substring(n, 1);
n = rotor2.IndexOf(sOutput) - ii;
validateN();
sOutput = alpha.Substring(n, 1);
n = alpha.IndexOf(sOutput) + iii;
validateN();
sOutput = alpha.Substring(n, 1);
n = rotor3.IndexOf(sOutput) - iii;
validateN();
sOutput = alpha.Substring(n, 1);
numericUpDown3.Value = iii + 1;
numericUpDown2.Value = ii + 1;
numericUpDown1.Value = i + 1;
tbxOutput.Text += sOutput;
}
private void validateN()
{
if (n < 0)
n = n + 26;
if (n > 25)
n = n - 26;
if (n < 0 || n > 25)
validateN();
}
private void Increment_Rotors()
{
iii++;
if (iii > 25)
iii = iii - 26;
if (ii == (notch2 - 1))
{
i++;
ii++;
}
else
{
if (iii == notch3)
{
ii++;
}
}
if (ii > 25)
ii = ii - 26;
if (i > 25)
i = i - 26;
}
我的问题:如果输入"TEST",为什么选择"OLPF"?不应该是"OWPF"吗?
祝你今天过得愉快! :)
My question: Why "OLPF" if I type "TEST"? Shouldn''t it be "OWPF"?
Have a nice day! :)
推荐答案
这篇关于C#谜题,有错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!