Exemple #1
0
        /// <summary>
        /// 得到结果
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string Translate(string input)
        {
            string result = "";

            //从第二位字符开始要变更转子位置
            rotorPosition -= prevLength;
            input          = input.ToUpper();
            foreach (char letter in input)
            {
                if (!alphabet.Contains(letter))
                {
                    continue;
                }
                //得到字母序号
                int charValue = alphaToNumber[letter];
                //连线板替换
                charValue = plugboard.Pass(charValue);
                //三次替换计算
                charValue = rotor1.Forward(charValue, RotorPosition1());
                charValue = rotor2.Forward(charValue, RotorPosition2());
                charValue = rotor3.Forward(charValue, RotorPosition3());
                //反射器
                charValue = reflector.Reflect(charValue);
                //原路返回
                charValue = rotor3.Backward(charValue, RotorPosition3());
                charValue = rotor2.Backward(charValue, RotorPosition2());
                charValue = rotor1.Backward(charValue, RotorPosition1());
                //返回插线板
                charValue = plugboard.Pass(charValue);
                //结果
                result += alphabet[charValue];
                //每个字符都会让转子移动
                rotorPosition++;
            }
            //明文长度
            prevLength = result.Length;
            return(result);
        }