Exemplo n.º 1
0
        private byte[] DecryptIndexBlock(string encryptionKey)
        {
            byte[] indexBlock;

            using (var reader = new BinaryReader(_stream, Encoding.UTF8, true))
            {
                _stream.Seek(_indexOffset, SeekOrigin.Begin);
                indexBlock = reader.ReadBytes((int)_indexSize);

                try
                {
                    var aes = new AesManaged
                    {
                        Mode    = CipherMode.ECB,
                        Padding = PaddingMode.Zeros,
                        Key     = encryptionKey.ConvertHexStringToBytes()
                    };
                    aes.CreateDecryptor().TransformBlock(indexBlock, 0, indexBlock.Length, indexBlock, 0);
                }
                catch (Exception ex) when(ex is ArgumentException || ex is FormatException || ex is CryptographicException)
                {
                    _entryTable = null;
                    throw new IncorrectEncryptionKeyException();
                }
            }

            return(indexBlock);
        }
Exemplo n.º 2
0
        private void ReadEntries()
        {
            _entryTable = new PakEntryTable();

            using (var reader = new BinaryReader(_stream, Encoding.UTF8, true))
            {
                _stream.Seek(_indexOffset, SeekOrigin.Begin);
                MountPoint = reader.ReadFString();
                int fileCount = reader.ReadInt32();
                for (int i = 0; i < fileCount; i++)
                {
                    string           fileName       = reader.ReadFString();
                    long             metadataOffset = _stream.Position;
                    PakEntryMetadata metadata       = ReadEntryMetadata(reader);
                    metadata.MetadataOffset = metadataOffset;
                    metadata.FileName       = fileName;

                    _entryTable.Add(metadata);
                }
            }
        }
Exemplo n.º 3
0
        public static VirtualFileSystem CreateGraph(PakEntryTable entries)
        {
            var graph = new VirtualFileSystem();

            foreach (PakEntryMetadata entry in entries)
            {
                var      currentFolder = graph.Root;
                string[] segments      = entry.FileName.Split('/');
                var      path          = new StringBuilder();
                for (int i = 0; i < segments.Length - 1; i++)
                {
                    path.Append("/" + segments[i]);
                    if (!currentFolder.Subfolders.ContainsKey(segments[i]))
                    {
                        currentFolder.Subfolders.Add(segments[i], new VirtualFolder(path.ToString()));
                    }
                    currentFolder = currentFolder.Subfolders[segments[i]];
                }
                currentFolder.Files.Add(segments[segments.Length - 1], entry);
            }

            return(graph);
        }
Exemplo n.º 4
0
        private void ReadEntries(string encryptionKey)
        {
            if (!IsIndexEncrypted)
            {
                ReadEntries();
                return;
            }

            _entryTable = new PakEntryTable();

            byte[] indexBlock = DecryptIndexBlock(encryptionKey);

            using (var reader = new BinaryReader(new MemoryStream(indexBlock)))
            {
                try
                {
                    MountPoint = reader.ReadFString();
                    int fileCount = reader.ReadInt32();
                    for (int i = 0; i < fileCount; i++)
                    {
                        string           fileName       = reader.ReadFString();
                        long             metadataOffset = reader.BaseStream.Position;
                        PakEntryMetadata metadata       = ReadEntryMetadata(reader);
                        metadata.MetadataOffset = metadataOffset + _indexOffset;
                        metadata.FileName       = fileName;

                        _entryTable.Add(metadata);
                    }
                }
                catch (ArgumentOutOfRangeException) // reader.ReadFString will throw this if MountPoint cannot be read
                {
                    _entryTable = null;
                    throw new IncorrectEncryptionKeyException();
                }
            }
        }