H
H
holymotion2016-02-09 22:02:30
C++ / C#
holymotion, 2016-02-09 22:02:30

Why does the search for characters in a byte matrix not work correctly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
    class Program
    {
        static List<byte> URusa = new List<byte>(256);
        static List<byte> temp2 = new List<byte>(256);
        static byte[,] URusa2 = new byte[16, 16];

        static string crypt, decrypt, _temp;
        static int t, x1, x2, y1, y2;

        public static void SimbolPoc(char s, out int x, out int y)
        {
            x = 0;
            y = 0;

            for (var i = 0; i < 16; i++)
            {
                for (var j = 0; j < 16; j++)
                {
                    if (s == URusa2[i, j])
                    {
                        x = j; y = i;
                    }
                }
            }
        }

        public static string Playfair_crypt(string s, string key)
        {
            byte[] key2 = Encoding.GetEncoding(1251).GetBytes(key);

            byte k = 0;
            for (var i = 0; i <= 255; i++)
            {
                URusa.Add(k);
                k++;
            }
            /*********************************************************/
            for (var i = 0; i < key2.Length; i++)
            {
                if (!temp2.Contains(key2[i]))
                {
                    temp2.Add(key2[i]);
                }
            }
            for (var i = 0; i < URusa.Count; i++)
            {
                if (!temp2.Contains(URusa[i]))
                {
                    temp2.Add(URusa[i]);
                }
            }
            t = 0;
            for (var i = 0; i < 16; i++)
            {
                for (var j = 0; j < 16; j++)
                {
                    URusa2[i, j] = temp2[t];
                    t++;
                    Console.Write("{0} ", URusa2[i, j]);
                }
                Console.WriteLine();
            }
            //просмотр строки по парам символов и вставка разделяющего символа
            //"ъ" в случае когда в паре попались одинаковые символы.
            for (var i = 0; i < s.Length / 2; i++)
            {
                if (s[2 * i] == s[2 * i + 1])
                {
                    s = s.Insert(2 * i + 1, "ъ");
                }
            }

            if (s.Length % 2 == 1)
            {
                if (s[s.Length - 1] != 'ъ')
                    s = s + 'ъ';
                else
                    s = s + 'я';
            }

            _temp = "";

            //Применение правил для шифра Плейфейра
            for (var i = 0; i < s.Length / 2; i++)
            {
                SimbolPoc(s[2 * i], out x1, out y1);
                SimbolPoc(s[2 * i + 1], out x2, out y2);
                if (y1 == y2)
                {
                    x1++; x2++;
                    if (x1 >= 16) x1 = x1 - 16;
                    if (x2 >= 16) x2 = x2 - 16;
                    _temp = _temp + URusa2[y1, x1] + URusa2[y2, x2];
                }
                //Правило2
                if (x1 == x2)
                {
                    y1++;
                    y2++;
                    if (y1 >= 16) y1 = y1 - 16;
                    if (y2 >= 16) y2 = y2 - 16;
                    _temp = _temp + URusa2[y1, x1] + URusa2[y2, x2];
                }
                //Правило3
                if ((x1 != x2) && (y1 != y2)) _temp = _temp + URusa2[y1, x2] + URusa2[y2, x1];
            }
            crypt = _temp;
            return crypt;
        }
        public static string Playfair_decrypt(string s, string key)
        {
            byte[] key2 = Encoding.GetEncoding(1251).GetBytes(key);

            byte k = 0;
            for (var i = 0; i <= 255; i++)
            {
                URusa.Add(k);
                k++;
            }
            /*********************************************************/
            for (var i = 0; i < key2.Length; i++)
            {
                if (!temp2.Contains(key2[i]))
                {
                    temp2.Add(key2[i]);
                }
            }
            for (var i = 0; i < URusa.Count; i++)
            {
                if (!temp2.Contains(URusa[i]))
                {
                    temp2.Add(URusa[i]);
                }
            }
            t = 0;
            for (var i = 0; i < 16; i++)
            {
                for (var j = 0; j < 16; j++)
                {
                    URusa2[i, j] = temp2[t];
                    t++;
                }
            }
            //просмотр строки по парам символов и вставка разделяющего символа
            //"ъ" в случае когда в паре попались одинаковые символы.

            _temp = "";
            //Применение правил для шифра Плейфейра
            for (var i = 0; i < s.Length / 2; i++)
            {
                SimbolPoc(s[2 * i], out x1, out y1);
                SimbolPoc(s[2 * i + 1], out x2, out y2);
                if (y1 == y2)
                {
                    x1--;
                    x2--;
                    if (x1 < 0) x1 = x1 + 16;
                    if (x2 < 0) x2 = x2 + 16;
                    _temp = _temp + URusa2[y1, x1] + URusa2[y2, x2];
                }
                //Правило2
                if (x1 == x2)
                {
                    y1--;
                    y2--;
                    if (y1 < 0) y1 = y1 - 16;
                    if (y2 < 0) y2 = y2 - 16;
                    _temp = _temp + URusa2[y1, x1] + URusa2[y2, x2];
                }
                //Правило3
                if ((x1 != x2) && (y1 != y2)) _temp = _temp + URusa2[y1, x2] + URusa2[y2, x1];
            }
            decrypt = _temp;
            return decrypt;
        }
        static void Main(string[] args)
        {
            Console.WriteLine(Playfair_crypt("Россия", "Форум"));
             //Console.WriteLine(Playfair_decrypt(Playfair_crypt("Россик", "Форум"), "Форум"));
        }
    }
}

In general, the Playfair cipher. The essence of the task: I have - Key, String. I create a byte array, write the key to the beginning of my array, and I get a byte matrix - this is what I did. But when you need to encrypt, problems arise, that is, the SimbolPoc function is launched (I pass it my line character and x1, y1) - which looks for a character in a byte matrix and returns its row and column number, if it finds it. And then I have some kind of nonsense going on with encryption, the symbol is not searched correctly, it gives out something else all the time.
Himself badi help me plz!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2016-02-09
@vilgeforce

Debugger in hand and go ahead, see what exactly is happening with you.

A
ar4ebaldello, 2016-02-10
@ar4ebaldello

If your implementation of SimbolPoc does not contain a symbol, you return indexes [0, 0], but this is the index of the first element, and not a sign of the absence of an element. I propose to rethink the algorithm a little and use the more logical TryGetCharIndices method, which, in addition to indexes, returns a search success bool.

static bool TryGetCharIndices(char element, out int x, out int y)
{
    for (var i = 0; i < URusa2.GetLength(0); i++)
    {
        for (var j = 0; j < URusa2.GetLength(1); j++)
        {
            if (URusa2[i, j] == element)
            {
                y = i;
                x = j;
                return true;
            }
        }
    }

    x = -1;
    y = -1;
    return false;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question