コード例 #1
0
        public static string EncryptText(Rottor[] rottors, Rottor reflector, string text)
        {
            string encryptedText = "";

            foreach (char letter in text)
            {
                rottors        = SwitchRottors(rottors);
                encryptedText += EncryptLetter(rottors, reflector, letter);
            }

            return(encryptedText);
        }
コード例 #2
0
        public static char EncryptLetter(Rottor[] rottors, Rottor reflector, char letter)
        {
            letter = SumChars(letter, rottors[0].CurrentLetter);
            letter = rottors[0].GetCharForward(letter);
            letter = SumChars(letter, SubChars(rottors[1].CurrentLetter, rottors[0].CurrentLetter));
            letter = rottors[1].GetCharForward(letter);
            letter = SumChars(letter, SubChars(rottors[2].CurrentLetter, rottors[1].CurrentLetter));
            letter = rottors[2].GetCharForward(letter);
            letter = SubChars(letter, rottors[2].CurrentLetter);

            letter = reflector.GetCharForward(letter);

            letter = SumChars(letter, rottors[2].CurrentLetter);
            letter = rottors[2].GetCharBack(letter);
            letter = SubChars(letter, SubChars(rottors[2].CurrentLetter, rottors[1].CurrentLetter));
            letter = rottors[1].GetCharBack(letter);
            letter = SubChars(letter, SubChars(rottors[1].CurrentLetter, rottors[0].CurrentLetter));
            letter = rottors[0].GetCharBack(letter);
            letter = SubChars(letter, rottors[0].CurrentLetter);

            return(letter);
        }
コード例 #3
0
        public void Process()
        {
            NetworkStream stream = null;

            try
            {
                stream = client.GetStream();
                byte[] data = new byte[64]; // буфер для получаемых данных
                while (true)
                {
                    // получаем сообщение
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    do
                    {
                        bytes = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (stream.DataAvailable);

                    string message = builder.ToString();
                    if (message.Length > 0)
                    {
                        Console.WriteLine(message);
                    }

                    char[] alph    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
                    char[] layer1  = "EKMFLGDQVZNTOWYHXUSPAIBRCJ".ToCharArray();
                    char[] layer2  = "AJDKSIRUXBLHWTMCQGZNPYFVOE".ToCharArray();
                    char[] layer3  = "BDFHJLCPRTXVZNYEIWGAKMUSQO".ToCharArray();
                    char[] reflect = "YRUHQSLDPXNGOKMIEBFZCWVJAT".ToCharArray();

                    Rottor[] rottors =
                    {
                        new Rottor(message[0], 'R',  alph, layer1),
                        new Rottor(message[1], 'V',  alph, layer2),
                        new Rottor(message[2], alph, layer3)
                    };

                    Rottor reflector = new Rottor('A', alph, reflect);

                    message = EnigmaAlg.EncryptText(rottors, reflector, message.Substring(3).ToUpper());


                    data = Encoding.Unicode.GetBytes(message);
                    stream.Write(data, 0, data.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }