Exemplo n.º 1
0
        /// <summary>
        /// Шифратор
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="cipher"></param>
        /// <returns></returns>
        private static byte[] Encrypt(string data, byte[] key, string cipher)
        {
            byte[] result;
            int    sizeBlock;

            switch (cipher)
            {
            case "Нет":
                result = Encoding.GetEncoding(866).GetBytes(data);
                break;

            case "Кузнечик":
                Kuznyechik kuznyechik = new Kuznyechik();
                sizeBlock = 16;

                //Если количество блоков не пропорционально размеру блока, то дописываем символы заполнения
                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                //Кодируем текст в массив байт
                byte[] dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result = new byte[dataBytes.Length];

                //В цикле формируем блоки...
                for (int i = 0; i < dataBytes.Length; i += sizeBlock)
                {
                    byte[] data8bytes = new byte[sizeBlock];

                    for (int k = 0; k < sizeBlock; k++)
                    {
                        data8bytes[k] = dataBytes[k + i];
                    }

                    //...и зашфровывем эти блоки с помощью ключа
                    byte[] dataE8bytes = kuznyechik.encrypt(data8bytes, key);

                    for (int k = 0; k < sizeBlock; k++)
                    {
                        result[k + i] = dataE8bytes[k];
                    }
                }
                break;

            case "Магма":
                //Аналогично Кузнечику
                Magma magma = new Magma();
                sizeBlock = 8;

                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result    = magma.Encode(dataBytes, key);
                break;

            case "AES":
                //Аналогично Кузнечику
                sizeBlock = 16;

                Aes aes = Aes.Create();
                aes.Mode      = CipherMode.ECB;
                aes.KeySize   = 256;
                aes.BlockSize = sizeBlock * 8;
                aes.Key       = key;
                aes.Padding   = PaddingMode.Zeros;

                ICryptoTransform encryptor = aes.CreateEncryptor();

                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result    = encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
                break;

            default:
                result = null;
                break;
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Шифратор
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private Packet Encrypt(string data)
        {
            byte[] result;
            byte[] dataBytes;
            int    sizeBlock;

            string cipher = comboBox1.SelectedItem.ToString();

            switch (cipher)
            {
            case "Нет":
                result = Encoding.GetEncoding(866).GetBytes(data);
                break;

            case "Кузнечик":
                Kuznyechik kuznyechik = new Kuznyechik();
                sizeBlock = 16;

                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result    = new byte[dataBytes.Length];

                for (int i = 0; i < dataBytes.Length; i += sizeBlock)
                {
                    byte[] data8bytes = new byte[sizeBlock];

                    for (int k = 0; k < sizeBlock; k++)
                    {
                        data8bytes[k] = dataBytes[k + i];
                    }

                    byte[] dataE8bytes = kuznyechik.encrypt(data8bytes, key);

                    for (int k = 0; k < sizeBlock; k++)
                    {
                        result[k + i] = dataE8bytes[k];
                    }
                }
                break;

            case "Магма":
                Magma magma = new Magma();
                sizeBlock = 8;

                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result    = magma.Encode(dataBytes, key);
                break;

            case "AES":
                sizeBlock = 16;

                Aes aes = Aes.Create();
                aes.Mode      = CipherMode.ECB;
                aes.KeySize   = 256;
                aes.BlockSize = sizeBlock * 8;
                aes.Key       = key;
                aes.Padding   = PaddingMode.Zeros;

                ICryptoTransform encryptor = aes.CreateEncryptor();

                while (data.Length % sizeBlock != 0)
                {
                    data += "┼";
                }

                dataBytes = Encoding.GetEncoding(866).GetBytes(data);
                result    = encryptor.TransformFinalBlock(dataBytes, 0, sizeBlock);
                break;

            default:
                result = null;
                break;
            }

            Packet packet = new Packet();

            packet.data   = result;
            packet.cipher = cipher;
            return(packet);
        }