Пример #1
0
        public void DecryptFirstChunkCorrectly()
        {
            byte[] encrypted = new byte[]
            {
                0x90, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x2D, 0x00, 0x00, 0x83, 0x1E, 0x4E, 0x60,
                0xBE, 0xE9, 0xE8, 0x94, 0xE9, 0x51, 0x8B, 0x2E, 0x9E, 0xDF, 0x9E, 0x69, 0xCC, 0x8B, 0x55, 0x79
            };
            byte[] expected = new byte[]
            {
                0x78, 0x9C, 0xB5, 0x7D, 0x07, 0x9C, 0x14, 0x45, 0xF6, 0x7F, 0x75, 0x4F, 0x0E, 0xBB, 0x3B, 0x9B
            };

            var data = EgsEncryption.Decrypt(new MemoryStream(encrypted));

            Assert.Equal(ToString(expected), ToString(data[0..16]));
Пример #2
0
        public void GenerateKeyCorrectly()
        {
            byte[] expected = new byte[]
            {
                0x90, 0x94, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xF0, 0x2D, 0x0A, 0x0B, 0x83, 0x1E, 0x4E, 0x60,
                0x1F, 0x61, 0x9A, 0xEE, 0x1B, 0x64, 0x9C, 0xE9, 0xEB, 0x49, 0x96, 0xE2, 0x68, 0x57, 0xD8, 0x82,
                0xF5, 0x54, 0xEF, 0xB1, 0xEE, 0x30, 0x73, 0x58, 0x05, 0x79, 0xE5, 0xBA, 0x6D, 0x2E, 0x3D, 0x38,
                0xBF, 0x22, 0x88, 0xAC, 0x51, 0x12, 0xFB, 0xF4, 0x54, 0x6B, 0x1E, 0x4E, 0x39, 0x45, 0x23, 0x76,
                0x27, 0xA3, 0xD1, 0x10, 0x76, 0xB1, 0x2A, 0xE4, 0x22, 0xDA, 0x34, 0xAA, 0x1B, 0x9F, 0x17, 0xDC,
                0xB2, 0x42, 0x70, 0x15, 0xC4, 0xF3, 0x5A, 0xF1, 0xE6, 0x29, 0x6E, 0x5B, 0xFD, 0xB6, 0x79, 0x87,
                0xAB, 0xD8, 0xF7, 0xE8, 0x6F, 0x2B, 0xAD, 0x19, 0x89, 0x02, 0xC3, 0x42, 0x74, 0xB4, 0xBA, 0xC5,
                0x2E, 0x80, 0x83, 0x89, 0x41, 0xAB, 0x2E, 0x90, 0xC8, 0xA9, 0xED, 0xD2, 0xBC, 0x1D, 0x57, 0x17,
                0x7C, 0x68, 0x62, 0xF5, 0x3D, 0xC3, 0x4C, 0x65, 0xF5, 0x6A, 0xA1, 0xB7, 0x49, 0x77, 0xF6, 0xA0,
                0x80, 0xA8, 0x27, 0x58, 0xBD, 0x6B, 0x6B, 0x3D, 0x48, 0x01, 0xCA, 0x8A, 0x01, 0x76, 0x3C, 0x2A,
                0xEF, 0x8F, 0x32, 0xD0, 0x52, 0xE4, 0x59, 0xED, 0x1A, 0xE5, 0x93, 0x67, 0x1B, 0x93, 0xAF, 0x4D,
            };

            var seed = new byte[]
            {
                0x90, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x2D, 0x00, 0x00, 0x83, 0x1E, 0x4E, 0x60
            };

            Assert.Equal(ToString(expected), ToString(EgsEncryption.GenerateKey(seed, 10)));
        }
Пример #3
0
                private List <EgsHdAsset.RemasteredEntry> ReplaceRemasteredAssets(string originalFile, EgsHdAsset asset, FileStream pkgStream, byte[] seed, byte[] originalAssetData, byte[] originalUncompressedData)
                {
                    var newRemasteredHeaders   = new List <EgsHdAsset.RemasteredEntry>();
                    var relativePath           = GetRelativePath(originalFile, Path.Combine(InputFolder, ORIGINAL_FILES_FOLDER_NAME));
                    var baseRemasteredFolder   = Path.Combine(InputFolder, REMASTERED_FILES_FOLDER_NAME, relativePath);
                    var remasteredAssetsFolder = GetHDAssetFolder(baseRemasteredFolder);

                    var remasteredAssetFiles    = asset.Assets;
                    var allRemasteredAssetsData = new MemoryStream();

                    // 0x30 is the size of this header
                    var totalRemasteredAssetHeadersSize = (remasteredAssetFiles.Count() * 0x30);
                    var offsetPosition = originalUncompressedData.Length;

                    foreach (var remasteredAssetFile in remasteredAssetFiles)
                    {
                        var assetFilePath = Path.Combine(remasteredAssetsFolder, remasteredAssetFile);

                        var uncompressedData = new byte[] {};
                        var compressedData   = new byte[] {};
                        var encryptedData    = new byte[] {};
                        if (File.Exists(assetFilePath))
                        {
                            Console.WriteLine($"Replacing remastered file: {relativePath}/{remasteredAssetFile}");
                            uncompressedData = File.ReadAllBytes(assetFilePath);
                            compressedData   = asset.RemasteredAssetHeaders[remasteredAssetFile].CompressedLength > -1 ? CompressData(uncompressedData) : uncompressedData;
                            Console.WriteLine(asset.RemasteredAssetHeaders[remasteredAssetFile].CompressedLength);

                            //Read the remastered file regardless, to set the correct offset
                            encryptedData = asset.ReadRawRemasteredAsset(remasteredAssetFile);
                            encryptedData = asset.RemasteredAssetHeaders[remasteredAssetFile].CompressedLength > -2 ? EgsEncryption.Encrypt(compressedData, seed) : compressedData;
                        }
                        else
                        {
                            Console.WriteLine($"Keeping remastered file: {relativePath}/{remasteredAssetFile}");
                            encryptedData    = asset.ReadRawRemasteredAsset(remasteredAssetFile);
                            uncompressedData = new byte[asset.RemasteredAssetHeaders[remasteredAssetFile].DecompressedLength];
                        }

                        var currentOffset = totalRemasteredAssetHeadersSize + offsetPosition + 0x10;

                        var entryCompressedLength = encryptedData.Length;
                        if (asset.RemasteredAssetHeaders[remasteredAssetFile].CompressedLength == -1)
                        {
                            entryCompressedLength = (int)-1;                                                                                                  // -1 is uncompressed
                        }
                        if (asset.RemasteredAssetHeaders[remasteredAssetFile].CompressedLength == -2)
                        {
                            entryCompressedLength = (int)-2;                                                                                                  // -2 is uncompressed and unencrypted
                        }
                        var remasteredEntry = new EgsHdAsset.RemasteredEntry()
                        {
                            CompressedLength   = entryCompressedLength,
                            DecompressedLength = (int)uncompressedData.Length,
                            Name      = remasteredAssetFile,
                            Offset    = currentOffset,
                            Unknown24 = asset.RemasteredAssetHeaders[remasteredAssetFile].Unknown24
                        };

                        newRemasteredHeaders.Add(remasteredEntry);

                        // Write asset header in the PKG stream
                        BinaryMapping.WriteObject <EgsHdAsset.RemasteredEntry>(pkgStream, remasteredEntry);

                        // Don't write into the PKG stream yet as we need to write
                        // all HD assets header juste after original file's data
                        allRemasteredAssetsData.Write(encryptedData);
                        if (encryptedData.Length % 0x10 != 0)
                        {
                            allRemasteredAssetsData.Write(Enumerable.Repeat((byte)0xCD, 16 - (encryptedData.Length % 0x10)).ToArray());
                        }

                        offsetPosition += uncompressedData.Length;
                    }

                    pkgStream.Write(originalAssetData);
                    pkgStream.Write(allRemasteredAssetsData.ReadAllBytes());

                    return(newRemasteredHeaders);
                }
Пример #4
0
                private Hed.Entry ReplaceFile(string filename, string inputFolder, FileStream hedStream, FileStream pkgStream, EgsHdAsset asset, Hed.Entry originalHedHeader = null)
                {
                    var filesToReplace   = GetAllFiles(inputFolder);
                    var originalfilename = GetRelativePath(filename, Path.Combine(InputFolder, ORIGINAL_FILES_FOLDER_NAME));

                    byte[] data           = new byte[asset.OriginalAssetHeader.DecompressedLength];
                    byte[] compressedData = new byte[] {};
                    byte[] encryptedData  = new byte[] {};
                    var    header         = CreateAssetHeader(
                        data,
                        asset.OriginalAssetHeader.CompressedLength,
                        asset.OriginalAssetHeader.RemasteredAssetCount,
                        asset.OriginalAssetHeader.Unknown0c
                        );
                    var encryptionKey = asset.Key;

                    if (filesToReplace.Contains(originalfilename))
                    {
                        Console.WriteLine($"Replacing original: {originalfilename}!");
                        Console.WriteLine(header.CompressedLength);
                        // Read Raw data to set the correct offset even if we're reading from an external file
                        data = asset.ReadRawData();
                        header.DecompressedLength = data.Length;
                        data = File.ReadAllBytes(filename);
                        header.DecompressedLength = data.Length;
                        compressedData            = header.CompressedLength > -1 ? CompressData(data) : data;
                        if (header.CompressedLength > -1)
                        {
                            header.CompressedLength = compressedData.Length;
                        }
                        Console.WriteLine(header.CompressedLength);
                        if (header.CompressedLength > -2)
                        {
                            var seed = new MemoryStream();

                            // The seed used for encryption is the data header6
                            BinaryMapping.WriteObject <EgsHdAsset.Header>(seed, header);
                            encryptionKey = seed.ReadAllBytes();

                            // Encrypt file
                            encryptedData = header.CompressedLength > 2 ? EgsEncryption.Encrypt(compressedData, encryptionKey) : compressedData;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Keeping original: {originalfilename}!");
                        //data = asset.ReadRawData();
                        compressedData = asset.ReadRawData();
                        encryptedData  = compressedData;
                    }
                    var offset = pkgStream.Position;

                    BinaryMapping.WriteObject <EgsHdAsset.Header>(pkgStream, header);

                    var remasteredHeaders = new List <EgsHdAsset.RemasteredEntry>();

                    // Is there remastered assets?
                    if (header.RemasteredAssetCount > 0)
                    {
                        remasteredHeaders = ReplaceRemasteredAssets(originalfilename, asset, pkgStream, encryptionKey, encryptedData, data);

                        // If a remastered asset is not replaced, we still want to count its size for the HED entry

                        foreach (var remasteredAssetName in asset.RemasteredAssetHeaders.Keys)
                        {
                            if (!remasteredHeaders.Exists(header => header.Name == remasteredAssetName))
                            {
                                remasteredHeaders.Add(asset.RemasteredAssetHeaders[remasteredAssetName]);
                            }
                        }
                    }
                    else
                    {
                        // Make sure to write the original file after remastered assets headers
                        pkgStream.Write(encryptedData);
                    }

                    // Write a new entry in the HED stream
                    var hedEntry = CreateHedEntry(originalfilename, data, (int)(pkgStream.Position - offset), offset, remasteredHeaders);

                    BinaryMapping.WriteObject <Hed.Entry>(hedStream, hedEntry);

                    return(hedEntry);
                }