Пример #1
0
 public void EncodeDecodeBaseN()
 {
     byte testByte = 157;
     List<byte> bytes = new List<byte>();
     for (uint radix = 2; radix < 1000; radix++)
     {
         var baseN = new BaseN(StringGenerator.GetAlphabet((int)radix), 64);
         int testBytesCount = Math.Max((baseN.BlockBitsCount + 7) / 8, (int)baseN.BlockCharsCount);
         bytes.Clear();
         for (int i = 0; i <= testBytesCount + 1; i++)
         {
             var array = bytes.ToArray();
             var encoded = baseN.Encode(array);
             var decoded = baseN.Decode(encoded);
             CollectionAssert.AreEqual(array, decoded);
             bytes.Add(testByte);
         }
     }
 }
Пример #2
0
        public void EncodeDecodeBaseN()
        {
            byte        testByte = 157;
            List <byte> bytes    = new List <byte>();

            for (uint radix = 2; radix < 1000; radix++)
            {
                var baseN          = new BaseN(StringGenerator.GetAlphabet((int)radix), 64);
                int testBytesCount = Math.Max((baseN.BlockBitsCount + 7) / 8, (int)baseN.BlockCharsCount);
                bytes.Clear();
                for (int i = 0; i <= testBytesCount + 1; i++)
                {
                    var array   = bytes.ToArray();
                    var encoded = baseN.Encode(array);
                    var decoded = baseN.Decode(encoded);
                    CollectionAssert.AreEqual(array, decoded);
                    bytes.Add(testByte);
                }
            }
        }
Пример #3
0
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            Stream         FStream         = null;

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter           = "txt files (*.txt)|*.txt";
            openFileDialog1.FilterIndex      = 2;
            openFileDialog1.RestoreDirectory = true;


            // Составляем таблицы для прямого и обратного преобразования
            byte cod    = 0;
            char sumbol = 'А';

            for (int i = 0; i < 16; i++)
            {
                Table.Add(cod, sumbol);     // Ключ - бит, значение - символ
                InvTable.Add(sumbol, cod);  // Ключ - символ, значение - бит
                cod++;
                sumbol++;
            }


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((FStream = openFileDialog1.OpenFile()) != null)
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

            Stream fs = openFileDialog1.OpenFile();

            byte[]       DecBytes  = new byte[fs.Length - 3];
            BinaryReader BinReader = new BinaryReader(fs);

            while (fs.Position < fs.Length)
            {
                if (fs.Position < 3)
                {
                    BinReader.ReadByte();
                    continue;
                }
                DecBytes[fs.Position - 3] = (byte)(BinReader.ReadByte());
            }
            BinReader.Close();
            fs.Close();



            //Получаем строку и удаляем из нее символы форматирования
            string Log_txt = Encoding.UTF8.GetString(DecBytes, 0, DecBytes.Length);

            Log_txt = Log_txt.Replace("\n", "");
            Log_txt = Log_txt.Replace(" ", "");

            string strHash = Log_txt.Substring(0, 26);
            var    refDec  = Log_txt.Substring(26, Log_txt.Length - 26);
            BaseN  triOd   = new BaseN();

            byte[] alf = triOd.Decode(refDec);

            /*// Обратное преобразование
             * foreach (char letter in Log_txt)
             * {
             *  Bytes.Add(InvTable[letter]);    // Создаем список полубайт
             * }
             * byte[] WrtBytes = new byte[Bytes.Count()/2];
             * for (int i = 0; i < Bytes.Count(); i += 2)
             * {
             *  byte buf = (byte)((Bytes[i] << 4) + (Bytes[i+1]));  // Складываем два полубайта в байт
             *  WrtBytes[i / 2] = buf;
             * }
             */

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Value   = 0;
            for (int i = 0; i < 100; i++)
            {
                progressBar1.Value++;
                System.Threading.Thread.Sleep(12);
            }
            System.Threading.Thread.Sleep(400);

            Stream         FWStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "ENC files (*.ENC)|*.ENC";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((FWStream = saveFileDialog1.OpenFile()) != null)
                {
                    BinaryWriter BinWriter = new BinaryWriter(FWStream);
                    BinWriter.Write(alf);
                    BinWriter.Close();
                    FWStream.Close();
                }
            }

            byte[] md5Hash = ComputeMD5Checksum(saveFileDialog1.FileName);
            BaseN  Hachcod = new BaseN();

            if (strHash != Hachcod.Encode(md5Hash))
            {
                MessageBox.Show("Файл поврежден. Хэш суммы не равны!!!");
            }
            //progressBar1.Value = 0;
            Table.Clear();
            InvTable.Clear();
        }