示例#1
0
        public ECB(byte BlockLenth, byte subBlocks, string pass)
            : base(BlockLenth, subBlocks, pass)
        {
            inds        = new int[this.SubBlocks];
            keysDecrypt = new byte[this.SubBlocks];
            for (int i = 0; i < this.SubBlocks; i++)
            {
                inds[i] = -1;
            }
            var gen = new CongruentialGenerator(MaHash8v64.GetHashCode(this.Password));

            for (int i = 0; i < this.SubBlocks - 1; i++)
            {
                byte newValue;
                do
                {
                    newValue = (byte)gen.Next(0, this.SubBlocks - 1);
                } while (inds[newValue] != -1 || (newValue == 0 && i == 0));
                inds[newValue] = i;
            }
            for (int i = 0; i < this.SubBlocks; i++)
            {
                if (inds[i] == -1)
                {
                    inds[i] = this.SubBlocks - 1;
                }
                keysDecrypt[i] = keys[inds[i]];
            }
        }
示例#2
0
        protected override void GenKeys(string key)
        {
            var gen = new CongruentialGenerator(MaHash8v64.GetHashCode(key));

            keys = new byte[this.SubBlocks];
            for (var i = 0; i < keys.Length - 1; i++)
            {
                keys[i] = (byte)gen.Next(1, this.BlockLenth - keys.Sum(a => a) - (this.SubBlocks - i - 1));
            }
            keys[keys.Length - 1] = (byte)(this.BlockLenth - keys.Sum(a => a));
        }
示例#3
0
        /// <summary>
        /// Общая функция преобразования входного массива байтов
        /// </summary>
        /// <param name="file">входной массив байтов</param>
        /// <param name="reverse">если false - шифрация, true - дешифрация</param>
        /// <param name="key">ключ</param>
        private void Crypt(string inputFile, string outputFile, string key)
        {
            var file = File.ReadAllBytes(inputFile);
            var gen  = new CongruentialGenerator(MaHash8v64.GetHashCode(key));
            var keys = new byte[Rounds];

            for (var i = 0; i < keys.Length; i++)
            {
                keys[i] = (byte)gen.Next(byte.MaxValue);
            }

            this.MaxValueProcess = (int)(file.LongLength * Rounds);



            byte[] l = new byte[BlockLenth / 2];
            byte[] r = new byte[BlockLenth / 2];

            var lenthBlocks = Math.Truncate((double)(file.LongLength / BlockLenth));

            for (byte k = 0; k < Rounds; k++)
            {
                for (var i = 0; i < lenthBlocks; i++)
                {
                    l = file.Skip(i * BlockLenth).Take(BlockLenth / 2).ToArray();
                    r = file.Skip(i * BlockLenth + BlockLenth / 2).Take(BlockLenth / 2).ToArray();
                    if (i < Rounds - 1) // если не последний раунд
                    {
                        byte[] t = l;
                        l = XOR(F(l, keys[k]), r);
                        r = t;
                    }
                    else // последний раунд
                    {
                        r = XOR(F(l, keys[k]), r);
                    }
                    for (int j = 0; j < BlockLenth / 2; j++)
                    {
                        file[i * BlockLenth + j] = l[j];
                        file[i * BlockLenth + j + BlockLenth / 2] = r[j];
                    }
                    this.CurrentValueProcess = i + k * Rounds;
                }
            }
            File.WriteAllBytes(outputFile, file);
        }