Beispiel #1
0
        private void WriteHeaderInCurrentPositionIfNotReadonly(EncryptedFile.Header fileHeader)
        {
            if (isReadonly)
            {
                return;
            }

            var headerBytes = StructConverter.ConvertStructToBits(fileHeader);

            stream.Write(headerBytes, 0, EncryptedFile.Header.HeaderSize);
        }
Beispiel #2
0
        private void WriteHeaderInCurrentPositionIfNotReadonly(EncryptedFile.Header fileHeader, ulong magicNumber)
        {
            if (isReadonly)
            {
                return;
            }

            var headerSize  = GetHeaderSizeByMagicNumber(magicNumber);
            var headerBytes = StructConverter.ConvertStructToBits(fileHeader, headerSize);

            stream.Write(headerBytes, 0, headerSize);
        }
Beispiel #3
0
        private EncryptedFile.Header ReadOrWriteHeader(int defaultBlockSize)
        {
            lock (locker)
            {
                stream.Position = 0;

                if (stream.Length >= EncryptedFile.Header.HeaderSize)
                {
                    // Read header

                    var headerBytes = stream.ReadEntireBlock(EncryptedFile.Header.HeaderSize);
                    var header      = StructConverter.ConvertBitsToStruct <EncryptedFile.Header>(headerBytes);

                    if (header.MagicNumber != EncryptedFile.DefaultMagicNumber)
                    {
                        throw new InvalidDataException("The magic number in the file doesn't match the expected magic number for encypted files. Perhaps this file isn't encrypted?");
                    }

                    return(header);
                }
                else
                {
                    // Write header

                    var sizeTest = settings.Codec.EncodeBlock("Dummy key", new byte[defaultBlockSize]);

                    var header = new EncryptedFile.Header
                    {
                        MagicNumber        = EncryptedFile.DefaultMagicNumber,
                        DecryptedBlockSize = defaultBlockSize,
                        IVSize             = sizeTest.IV.Length,
                        EncryptedBlockSize = sizeTest.Data.Length
                    };
                    var headerBytes = StructConverter.ConvertStructToBits(header);

                    if (!isReadonly)
                    {
                        stream.Write(headerBytes, 0, EncryptedFile.Header.HeaderSize);
                    }

                    return(header);
                }
            }
        }
Beispiel #4
0
        private EncryptedFile.Header ReadOrWriteEmptyHeader(int defaultBlockSize)
        {
            lock (locker)
            {
                stream.Position = 0;
                if (stream.Length >= EncryptedFile.Header.HeaderSize)
                {
                    // Read header
                    var magicNumber = stream.ReadUInt64();
                    var headerSize  = GetHeaderSizeByMagicNumber(magicNumber);

                    stream.Position = 0;
                    var headerBytes = stream.ReadEntireBlock(headerSize);
                    var fileHeader  = StructConverter.ConvertBitsToStruct <EncryptedFile.Header>(headerBytes, headerSize);

                    if (fileHeader.MagicNumber != EncryptedFile.DefaultMagicNumber && fileHeader.MagicNumber != EncryptedFile.WithTotalSizeMagicNumber)
                    {
                        throw new InvalidDataException("The magic number in the file doesn't match the expected magic number for encrypted files. Perhaps this file isn't encrypted?");
                    }

                    return(fileHeader);
                }
                else
                {
                    // Write empty header
                    var sizeTest = settings.Codec.EncodeBlock("Dummy key", new byte[defaultBlockSize]);

                    var fileHeader = new EncryptedFile.Header
                    {
                        MagicNumber          = EncryptedFile.WithTotalSizeMagicNumber,
                        DecryptedBlockSize   = defaultBlockSize,
                        IVSize               = sizeTest.IV.Length,
                        EncryptedBlockSize   = sizeTest.Data.Length,
                        TotalUnencryptedSize = 0
                    };

                    WriteHeaderInCurrentPositionIfNotReadonly(fileHeader, fileHeader.MagicNumber);

                    return(fileHeader);
                }
            }
        }