Example #1
0
        public byte[] ToBufferEncryptBlock()
        {
            byte[] result = new byte[16];
            CryptoRandom.GetBytes(result);
            Buffer.BlockCopy(BitConverter.GetBytes(_InfSize), 0, result, 13, 2);
            Buffer.BlockCopy(BitConverter.GetBytes((byte)ElType * 128 + (int)CryptoRandom.Random(128)), 0, result, 15, 1);

            return(result);
        }
Example #2
0
        public Header(SafeStreamAccess headersStream, AesCryptoServiceProvider AES, ElementType blockType)
        {
            _IV = new byte[16];
            CryptoRandom.GetBytes(_IV);

            _AES               = AES;
            _StartPos          = (UInt64)headersStream.Length;
            _Exists            = true;
            _ElType            = blockType;
            _headersFileStream = headersStream;
            _InfSize           = 0;
        }
Example #3
0
        //Створення файлу вручну
        public FileElement(DirElement parent, Header parentHeader, SafeStreamAccess dataFileStream, string Name, Stream fileStream, bool isCompressed,
                           Object addElementLocker, Object changeElementsLocker, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null) : base(addElementLocker, changeElementsLocker)
        {
            lock (_addElementLocker)
            {
                UInt64 fileSize = (UInt64)fileStream.Length;

                byte[] icon     = GetIconBytes(Icon);
                UInt32 iconSize = icon == null ? 0 : (UInt32)icon.Length;

                UInt64 fileStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(fileSize));                 //Вибираємо місце куди писати файл
                UInt64 iconStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(iconSize));                 //Вибираємо місце куди писати іконку
                iconStartPos = (iconStartPos == fileStartPos) ? iconStartPos += Crypto.GetMod16(fileSize) : iconStartPos;

                lock (dataFileStream.writeLock)
                {
                    header = new Header(parentHeader.headersFileStream, parentHeader.AES, ElementType.File);
                    this.dataFileStream = dataFileStream;

                    _Name         = Name;
                    _ParentID     = parent.ID;
                    _FileStartPos = fileStartPos;
                    _FileSize     = fileSize;
                    _IconStartPos = iconStartPos;
                    _IconSize     = iconSize;
                    _IsCompressed = isCompressed;
                    _Hash         = new byte[16];
                    CryptoRandom.GetBytes(_Hash);
                    _PHash = GetPHash(Icon);

                    SaveInf();
                    _Exists = false;
                }

                AesCryptoServiceProvider AES = GetFileAES(_FileIV);

                if (fileSize > 0)
                {
                    dataFileStream.WriteEncrypt((long)fileStartPos, fileStream, AES, out _Hash, Progress);
                }

                if ((icon != null) && (iconSize > 0))
                {
                    AES.IV = _IconIV;
                    dataFileStream.WriteEncrypt((long)iconStartPos, icon, AES);
                }

                //Закидаємо файл в потрібну папку і записуємо зміни
                _Exists = true;
                ChangeParent(parent, true);
            }
        }
Example #4
0
        protected static byte[] GetPHash(Bitmap Icon)
        {
            if (Icon == null)
            {
                byte[] phash = new byte[8];
                CryptoRandom.GetBytes(phash);
                return(phash);
            }

            try
            {
                return(_GetPHash(Icon));
            }
            catch
            {
                return(GetPHash(null));
            }
        }
Example #5
0
        public void ChangeContent(Stream NewData, SafeStreamAccess.ProgressCallback Progress = null)
        {
            lock (_addElementLocker)
            {
                UInt64 fileSize     = (UInt64)NewData.Length;
                UInt64 fileStartPos = _FileStartPos;
                if (fileSize >= Crypto.GetMod16(_FileSize))
                {
                    fileStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(fileSize));                     //Вибираємо місце куди писати файл
                }

                byte[] tempHash;

                try
                {
                    if (fileSize > 0)
                    {
                        AesCryptoServiceProvider AES = GetFileAES(_FileIV);
                        dataFileStream.WriteEncrypt((long)fileStartPos, NewData, AES, out tempHash, Progress);
                    }
                    else
                    {
                        tempHash = new byte[16];
                        CryptoRandom.GetBytes(tempHash);
                    }
                }
                catch
                {
                    throw new DataWasNotWrittenException("Файл не записався.");
                }

                try
                {
                    _FileStartPos = fileStartPos;
                    _FileSize     = fileSize;
                    _Hash         = tempHash;
                    SaveInf();
                }
                catch
                {
                    throw new HeaderWasNotWrittenException("Інформація про зміни не записалась!");
                }
            }
        }
Example #6
0
        public bool SaveInfo(byte[] info, int realLength)
        {
            CryptoRandom.GetBytes(info, realLength, info.Length - realLength);

            lock (_headersFileStream.writeLock)
            {
                if (info.Length > _InfSize)
                {
                    Delete();
                    _Exists   = true;
                    _StartPos = (UInt64)_headersFileStream.Length;
                    _InfSize  = (ushort)info.Length;

                    Save();
                }

                SetAESValue();

                _headersFileStream.WriteEncrypt((long)(_StartPos + Length), info, AES);
            }

            return(true);
        }