Exemple #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]];
            }
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #4
0
        /// <summary>
        /// Общая функция преобразования входного массива байтов
        /// </summary>
        /// <param name="inputFile">путь входного файла</param>
        /// <param name="outputFile">путь выходного файла</param>
        /// <param name="isEncrypt">если true - шифрация, false - дешифрация</param>
        /// <param name="key">ключ</param>
        protected virtual CryptResult Crypt(string inputFile, string outputFile, bool isEncrypt)
        {
            var inputstream = File.OpenRead(inputFile);
            var sr          = new BinaryReader(inputstream);

            FileStream   outputstream = null;
            BinaryWriter wr           = null;

            this.CurrentValueProcess = 0;
            this.MaxValueProcess     = (int)(inputstream.Length);

            byte crc         = 0xFF;
            byte crcDec      = 0;
            long crcPosition = 0;

            if (isEncrypt)
            {
                outputstream = File.OpenWrite(outputFile);
                wr           = new BinaryWriter(outputstream);
                var enc = MaHash8v64.GetHashCode(this.Password);
                // var bytesPass = Encoding.Unicode.GetBytes(enc);
                wr.Write(enc);
                var properties = this.GetType().GetProperties().Where(a => a.GetCustomAttributes(true).OfType <ParametrCryptAttribute>().Count() != 0).ToArray();
                foreach (var p in properties)
                {
                    wr.Write((byte)p.GetValue(this, null));
                }
                crcPosition = outputstream.Position;
                outputstream.Seek(1, SeekOrigin.Current);
            }
            else
            {
                var hashPass = sr.ReadUInt32();

                var properties = this.GetType().GetProperties().Where(a => a.GetCustomAttributes(true).OfType <ParametrCryptAttribute>().Count() != 0).ToArray();

                var paramsValue = new byte[properties.Length];
                for (var i = 0; i < properties.Length; i++)
                {
                    paramsValue[i] = sr.ReadByte();
                }
                if (hashPass != MaHash8v64.GetHashCode(this.Password))
                {
                    inputstream.Close();
                    return(CryptResult.MismatchValueParameters);
                }
                for (var i = 0; i < properties.Length; i++)
                {
                    if (paramsValue[i] != (byte)properties[i].GetValue(this, null))
                    {
                        inputstream.Close();
                        return(CryptResult.MismatchValueParameters);
                    }
                }
                crcDec = sr.ReadByte();
            }
            if (wr == null)
            {
                outputstream = File.OpenWrite(outputFile);
                wr           = new BinaryWriter(outputstream);
            }
            while (true)
            {
                var buffer = sr.ReadBytes(this.BlockLenth);
                if (!isEncrypt)
                {
                    crc = GetCRC(crc, buffer);
                }
                if (buffer.Length == BlockLenth)
                {
                    buffer = CryptBlock(isEncrypt, buffer);
                }
                if (isEncrypt)
                {
                    crc = GetCRC(crc, buffer);
                }
                if (buffer.Length != BlockLenth)
                {
                    this.CurrentValueProcess = this.MaxValueProcess - 1;
                    wr.Write(buffer);
                    break;
                }
                wr.Write(buffer);
                this.CurrentValueProcess += 10;
            }
            if (isEncrypt)
            {
                outputstream.Seek(crcPosition, SeekOrigin.Begin);
                wr.Write(crc);
            }
            else
            {
                if (crc != crcDec)
                {
                    inputstream.Close();
                    outputstream.Close();
                    return(CryptResult.MismatchСhecksum);
                }
            }
            inputstream.Close();
            outputstream.Close();
            return(CryptResult.Success);
        }