Пример #1
0
        /*
         * Main Enigma algorithm
         *
         * Here will be many comments with string report
         * it is artifact of debug, think it may be usefull in future
         */
        private char CipherChar(char sym)
        {
            // if uncomment it, it may crush
            if (!alphabet.Contains(Char.ToUpper(sym)))
            {
                return(sym);
            }

            sym = m_commutators.Commutate(sym);

            m_first.Rotate(1);   /* first rotor rotate on each letter */
            // symIndex always be index of main alphabet
            int symIndex = m_first.Alphabet.IndexOf(sym);

            // string report = $"[START] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";

            symIndex += m_first.RotorPosition;
            symIndex %= m_first.Alphabet.Length;

            if (m_first.StepLetters.Contains(m_first.Alphabet[m_first.RotorPosition]))
            {
                // report += "[SECOND ROTOR] Rotated by first\n";
                m_second.Rotate(1);
            }
            // report += $"[FIRST ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_first.CipherRightSignal(symIndex);
            // report += $"[FIRST ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_first.RotorPosition;
            symIndex += m_second.RotorPosition;
            symIndex += m_second.Alphabet.Length;
            symIndex %= m_second.Alphabet.Length;

            if (m_second.StepLetters.Contains(m_second.Alphabet[m_second.RotorPosition]))
            {
                // report += "[THIRD ROTOR] Rotated by second\n";
                m_third.Rotate(1);
            }
            // report += $"[SECOND ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_second.CipherRightSignal(symIndex);
            // report += $"[SECOND ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_second.RotorPosition;
            symIndex += m_third.RotorPosition;
            symIndex += m_third.Alphabet.Length;
            symIndex %= m_third.Alphabet.Length;


            // report += $"[THIRD ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_third.CipherRightSignal(symIndex);
            // report += $"[THIRD ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_third.RotorPosition;
            symIndex += m_third.Alphabet.Length;
            symIndex %= m_third.Alphabet.Length;


            // report += $"[REFLECTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_reflector.Reflect(symIndex);
            // report += $"[REFLECTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex += m_third.RotorPosition;
            symIndex %= m_third.Alphabet.Length;


            // report += $"[THIRD ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_third.CipherReflectedSignal(symIndex);
            // report += $"[THIRD ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_third.RotorPosition;
            symIndex += m_second.RotorPosition;
            symIndex += m_second.Alphabet.Length;
            symIndex %= m_second.Alphabet.Length;


            // report += $"[SECOND ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_second.CipherReflectedSignal(symIndex);
            // report += $"[SECOND ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_second.RotorPosition;
            symIndex += m_first.RotorPosition;
            symIndex += m_first.Alphabet.Length;
            symIndex %= m_first.Alphabet.Length;


            // report += $"[FIRST ROTOR INPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex = m_first.CipherReflectedSignal(symIndex);
            // report += $"[FIRST ROTOR OUTPUT] symindex = {symIndex}( {m_first.Alphabet[symIndex]} )\n";
            symIndex -= m_first.RotorPosition;
            symIndex += m_first.Alphabet.Length;
            symIndex %= m_first.Alphabet.Length;

            // report += $"[END]symIndex = {symIndex}, result = {m_first.Alphabet[symIndex]}\n";
            // MessageBox.Show(report);
            char notCommutatedResult = m_first.Alphabet[symIndex];

            // MessageBox.Show($"notCommutatedResult = {m_first.Alphabet[symIndex]}\n");
            return(m_commutators.Commutate(notCommutatedResult));
        }