Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] Decode(byte[] data, string key, string extension = "")
- {
- string @string = Encoding.UTF8.GetString(data, 0, 4);
- if (@string != ResourceFileElement.SIGNATURE)
- {
- Debug.LogError("not .fe");
- return null;
- }
- int num = (int)BitConverter.ToInt16(data, 12);
- int num2 = (int)BitConverter.ToInt16(data, 14);
- byte[] array = new byte[16];
- Array.Copy(data, 16, array, 0, 16);
- byte[] array2 = new byte[16];
- Array.Copy(data, 32, array2, 0, 16);
- int size = data.Length - 48;
- byte[] array3 = ResourceFileElement.DecodeAES(data, 48, size, array2, Encoding.UTF8.GetBytes(key));
- if (num2 > 0)
- {
- Array.Resize<byte>(ref array3, array3.Length - num2);
- }
- if (num == 1)
- {
- array3 = ResourceFileElement.DecompressGZip(array3);
- }
- byte[] data2 = null;
- using (MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider())
- {
- data2 = mD5CryptoServiceProvider.ComputeHash(array3);
- }
- if (Utility.Bin2Hex(array, -1) != Utility.Bin2Hex(data2, -1))
- {
- Debug.LogError("md5 check error.");
- return null;
- }
- string text = string.Empty;
- bool flag = false;
- switch (extension)
- {
- case ".unity3d":
- text = Encoding.UTF8.GetString(array3, 0, 8);
- if (text != "UnityWeb" && text != "UnityRaw")
- {
- flag = true;
- }
- break;
- case ".jpg":
- text = BitConverter.ToString(array3, 0, 2);
- if (text != "FF-D8")
- {
- flag = true;
- }
- break;
- case ".png":
- text = BitConverter.ToString(array3, 0, 4);
- if (text != "89-50-4E-47")
- {
- flag = true;
- }
- break;
- case ".acb":
- case ".acf":
- text = Encoding.UTF8.GetString(array3, 0, 4);
- if (text != "@UTF")
- {
- flag = true;
- }
- break;
- }
- if (flag)
- {
- Debug.LogError("signature = " + text + " error.");
- return null;
- }
- return array3;
- }
- public static byte[] DecompressGZip(byte[] data)
- {
- byte[] array = new byte[32768];
- MemoryStream memoryStream = new MemoryStream(data, 0, data.Length);
- MemoryStream memoryStream2 = new MemoryStream();
- GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
- int count;
- while ((count = gZipStream.Read(array, 0, array.Length)) > 0)
- {
- memoryStream2.Write(array, 0, count);
- }
- byte[] result = memoryStream2.ToArray();
- memoryStream2.Close();
- gZipStream.Close();
- memoryStream.Close();
- return result;
- }
- public static byte[] DecodeAES(byte[] data, int seek, int size, byte[] iv, byte[] key)
- {
- AesManaged aesManaged = new AesManaged();
- aesManaged.BlockSize = 128;
- aesManaged.KeySize = 128;
- aesManaged.Mode = CipherMode.CBC;
- aesManaged.Padding = PaddingMode.Zeros;
- aesManaged.IV = iv;
- aesManaged.Key = key;
- byte[] result = null;
- using (MemoryStream memoryStream = new MemoryStream(data, seek, size, false, true))
- {
- using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aesManaged.CreateDecryptor(), CryptoStreamMode.Read))
- {
- using (MemoryStream memoryStream2 = new MemoryStream())
- {
- byte[] array = new byte[32768];
- while (true)
- {
- int num = cryptoStream.Read(array, 0, array.Length);
- if (num <= 0)
- {
- break;
- }
- memoryStream2.Write(array, 0, num);
- }
- result = memoryStream2.ToArray();
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment