From cc7eb565181fcd1595175d29e89d47847046a40a Mon Sep 17 00:00:00 2001 From: MichaelO Date: Fri, 17 May 2024 21:37:12 +0800 Subject: [PATCH] updated to v2.11.1 --- .../Editor/Bundle/CryptogramSetting.cs | 6 + .../BundleCryptogramUtilityWindow.cs | 87 +++++++- .../EditorWindow/CryptogramSettingWindow.cs | 85 +++++++ .../Bundle/YooAssets/EncryptionServices.cs | 33 +++ .../Scripts/Runtime/Bundle/BundleConfig.cs | 1 + .../Scripts/Runtime/Bundle/BundleInfos.cs | 2 +- .../Scripts/Runtime/Bundle/FileCryptogram.cs | 207 +++++++++++++++++- .../Scripts/Runtime/Bundle/PackageManager.cs | 3 + .../Bundle/YooAssets/DecryptionServices.cs | 68 +++++- .../Runtime/Utility/CryptogramUtility.cs | 38 ++++ .../ThirdParty/YooAsset/CHANGELOG.md | 36 +++ .../ThirdParty/YooAsset/package.json | 2 +- Assets/OxGFrame/CHANGELOG.md | 12 +- Assets/OxGFrame/package.json | 2 +- .../Settings/OxGFrame/CryptogramSetting.asset | 10 +- README.md | 8 +- 16 files changed, 564 insertions(+), 36 deletions(-) diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/CryptogramSetting.cs b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/CryptogramSetting.cs index f4e6a62e..edadedec 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/CryptogramSetting.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/CryptogramSetting.cs @@ -18,6 +18,12 @@ public class CryptogramSetting : ScriptableObject public byte tXorKey = 1; public byte jXorKey = 1; + [Separator("HT2XORPlus")] + public byte hXorPlusKey = 1; + public byte tXorPlusKey = 1; + public byte j1XorPlusKey = 1; + public byte j2XorPlusKey = 1; + [Separator("AES")] public string aesKey = "aes_key"; public string aesIv = "aes_iv"; diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/BundleCryptogramUtilityWindow.cs b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/BundleCryptogramUtilityWindow.cs index 67b6f7ff..b6e6b75a 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/BundleCryptogramUtilityWindow.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/BundleCryptogramUtilityWindow.cs @@ -5,19 +5,12 @@ using UnityEditor; using UnityEngine; using YooAsset.Editor; +using static OxGFrame.AssetLoader.Editor.CryptogramSettingWindow; namespace OxGFrame.AssetLoader.Editor { public class BundleCryptogramUtilityWindow : EditorWindow { - public enum CryptogramType - { - Offset, - Xor, - HT2Xor, - Aes - } - private static BundleCryptogramUtilityWindow _instance = null; internal static BundleCryptogramUtilityWindow GetInstance() { @@ -60,12 +53,25 @@ private void OnEnable() private void _LoadSettingsData() { + // Offset this.randomSeed = this._setting.randomSeed; this.dummySize = this._setting.dummySize; + + // XOR this.xorKey = this._setting.xorKey; + + // HT2XOR this.hXorKey = this._setting.hXorKey; this.tXorKey = this._setting.tXorKey; this.jXorKey = this._setting.jXorKey; + + // HT2XORPlus + this.hXorPlusKey = this._setting.hXorPlusKey; + this.tXorPlusKey = this._setting.tXorPlusKey; + this.j1XorPlusKey = this._setting.j1XorPlusKey; + this.j2XorPlusKey = this._setting.j2XorPlusKey; + + // AES this.aesKey = this._setting.aesKey; this.aesIv = this._setting.aesIv; } @@ -118,12 +124,16 @@ private void _CryptogramType(CryptogramType cryptogramType) case CryptogramType.HT2Xor: this._DrawHT2XorView(); break; + case CryptogramType.HT2XorPlus: + this._DrawHT2XorPlusView(); + break; case CryptogramType.Aes: this._DrawAesView(); break; } } + #region Offset [SerializeField] public int randomSeed = 1; [SerializeField] @@ -155,7 +165,9 @@ private void _DrawOffsetView() EditorGUILayout.EndVertical(); } + #endregion + #region Xor [SerializeField] public int xorKey = 0; private void _DrawXorView() @@ -183,7 +195,9 @@ private void _DrawXorView() EditorGUILayout.EndVertical(); } + #endregion + #region HT2Xor [SerializeField] public int hXorKey = 0; [SerializeField] @@ -221,7 +235,55 @@ private void _DrawHT2XorView() EditorGUILayout.EndVertical(); } + #endregion + #region HT2XorPlus + [SerializeField] + public int hXorPlusKey = 0; + [SerializeField] + public int tXorPlusKey = 0; + [SerializeField] + public int j1XorPlusKey = 0; + [SerializeField] + public int j2XorPlusKey = 0; + private void _DrawHT2XorPlusView() + { + EditorGUILayout.Space(); + + GUIStyle style = new GUIStyle(); + var bg = new Texture2D(1, 1); + ColorUtility.TryParseHtmlString("#1e3836", out Color color); + Color[] pixels = Enumerable.Repeat(color, Screen.width * Screen.height).ToArray(); + bg.SetPixels(pixels); + bg.Apply(); + style.normal.background = bg; + EditorGUILayout.BeginVertical(style); + var centeredStyle = new GUIStyle(GUI.skin.GetStyle("Label")); + centeredStyle.alignment = TextAnchor.UpperCenter; + GUILayout.Label(new GUIContent("Head-Tail 2 XOR Plus Settings"), centeredStyle); + EditorGUILayout.Space(); + + EditorGUIUtility.labelWidth = 200; + this.hXorPlusKey = EditorGUILayout.IntField("Head XOR Plus KEY (0 ~ 255)", this.hXorPlusKey); + if (this.hXorPlusKey < 0) this.hXorPlusKey = 0; + else if (this.hXorPlusKey > 255) this.hXorPlusKey = 255; + this.tXorPlusKey = EditorGUILayout.IntField("Tail XOR Plus KEY (0 ~ 255)", this.tXorPlusKey); + if (this.tXorPlusKey < 0) this.tXorPlusKey = 0; + else if (this.tXorPlusKey > 255) this.tXorPlusKey = 255; + this.j1XorPlusKey = EditorGUILayout.IntField("Jump 1 XOR Plus KEY (0 ~ 255)", this.j1XorPlusKey); + if (this.j1XorPlusKey < 0) this.j1XorPlusKey = 0; + else if (this.j1XorPlusKey > 255) this.j1XorPlusKey = 255; + this.j2XorPlusKey = EditorGUILayout.IntField("Jump 2 XOR Plus KEY (0 ~ 255)", this.j2XorPlusKey); + if (this.j2XorPlusKey < 0) this.j2XorPlusKey = 0; + else if (this.j2XorPlusKey > 255) this.j2XorPlusKey = 255; + + this._DrawOperateButtonsView(this.cryptogramType); + + EditorGUILayout.EndVertical(); + } + #endregion + + #region AES [SerializeField] public string aesKey = "file_key"; [SerializeField] @@ -250,6 +312,7 @@ private void _DrawAesView() EditorGUILayout.EndVertical(); } + #endregion private void _DrawOperateButtonsView(CryptogramType cryptogramType) { @@ -273,6 +336,10 @@ private void _DrawOperateButtonsView(CryptogramType cryptogramType) CryptogramUtility.HT2XorDecryptBundleFiles(this.sourceFolder, (byte)this.hXorKey, (byte)this.tXorKey, (byte)this.jXorKey); EditorUtility.DisplayDialog("Crytogram Message", "[Head-Tail 2 XOR] Decrypt Process.", "OK"); break; + case CryptogramType.HT2XorPlus: + CryptogramUtility.HT2XorPlusDecryptBundleFiles(this.sourceFolder, (byte)this.hXorPlusKey, (byte)this.tXorPlusKey, (byte)this.j1XorPlusKey, (byte)this.j2XorPlusKey); + EditorUtility.DisplayDialog("Crytogram Message", "[Head-Tail 2 XOR Plus] Decrypt Process.", "OK"); + break; case CryptogramType.Aes: if (string.IsNullOrEmpty(this.aesKey) || string.IsNullOrEmpty(this.aesIv)) { @@ -304,6 +371,10 @@ private void _DrawOperateButtonsView(CryptogramType cryptogramType) CryptogramUtility.HT2XorEncryptBundleFiles(this.sourceFolder, (byte)this.hXorKey, (byte)this.tXorKey, (byte)this.jXorKey); EditorUtility.DisplayDialog("Crytogram Message", "[Head-Tail 2 XOR] Encrypt Process.", "OK"); break; + case CryptogramType.HT2XorPlus: + CryptogramUtility.HT2XorPlusEncryptBundleFiles(this.sourceFolder, (byte)this.hXorPlusKey, (byte)this.tXorPlusKey, (byte)this.j1XorPlusKey, (byte)this.j2XorPlusKey); + EditorUtility.DisplayDialog("Crytogram Message", "[Head-Tail 2 XOR Plus] Encrypt Process.", "OK"); + break; case CryptogramType.Aes: if (string.IsNullOrEmpty(this.aesKey) || string.IsNullOrEmpty(this.aesIv)) { diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/CryptogramSettingWindow.cs b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/CryptogramSettingWindow.cs index 5dedf490..db146c8e 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/CryptogramSettingWindow.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/EditorWindow/CryptogramSettingWindow.cs @@ -12,6 +12,7 @@ public enum CryptogramType Offset, Xor, HT2Xor, + HT2XorPlus, Aes } @@ -74,12 +75,25 @@ private void OnGUI() private void _LoadSettingsData() { + // Offset this.randomSeed = this._setting.randomSeed; this.dummySize = this._setting.dummySize; + + // XOR this.xorKey = this._setting.xorKey; + + // HT2XOR this.hXorKey = this._setting.hXorKey; this.tXorKey = this._setting.tXorKey; this.jXorKey = this._setting.jXorKey; + + // HT2XORPlus + this.hXorPlusKey = this._setting.hXorPlusKey; + this.tXorPlusKey = this._setting.tXorPlusKey; + this.j1XorPlusKey = this._setting.j1XorPlusKey; + this.j2XorPlusKey = this._setting.j2XorPlusKey; + + // AES this.aesKey = this._setting.aesKey; this.aesIv = this._setting.aesIv; } @@ -97,12 +111,16 @@ private void _CryptogramType(CryptogramType cryptogramType) case CryptogramType.HT2Xor: this._DrawHT2XorView(); break; + case CryptogramType.HT2XorPlus: + this._DrawHT2XorPlusView(); + break; case CryptogramType.Aes: this._DrawAesView(); break; } } + #region Offset [SerializeField] public int randomSeed = 1; [SerializeField] @@ -135,7 +153,9 @@ private void _DrawOffsetView() EditorGUILayout.EndVertical(); } + #endregion + #region Xor [SerializeField] public int xorKey = 0; private void _DrawXorView() @@ -165,7 +185,9 @@ private void _DrawXorView() EditorGUILayout.EndVertical(); } + #endregion + #region HT2Xor [SerializeField] public int hXorKey = 0; [SerializeField] @@ -205,7 +227,57 @@ private void _DrawHT2XorView() EditorGUILayout.EndVertical(); } + #endregion + + #region HT2Xor Plus + [SerializeField] + public int hXorPlusKey = 0; + [SerializeField] + public int tXorPlusKey = 0; + [SerializeField] + public int j1XorPlusKey = 0; + [SerializeField] + public int j2XorPlusKey = 0; + private void _DrawHT2XorPlusView() + { + EditorGUILayout.Space(); + + GUIStyle style = new GUIStyle(); + var bg = new Texture2D(1, 1); + ColorUtility.TryParseHtmlString("#1e3836", out Color color); + Color[] pixels = Enumerable.Repeat(color, Screen.width * Screen.height).ToArray(); + bg.SetPixels(pixels); + bg.Apply(); + style.normal.background = bg; + EditorGUILayout.BeginVertical(style); + var centeredStyle = new GUIStyle(GUI.skin.GetStyle("Label")); + centeredStyle.alignment = TextAnchor.UpperCenter; + GUILayout.Label(new GUIContent("Head-Tail 2 XOR Plus Settings"), centeredStyle); + EditorGUILayout.Space(); + + EditorGUI.BeginChangeCheck(); + EditorGUIUtility.labelWidth = 200; + this.hXorPlusKey = EditorGUILayout.IntField("Head XOR Plus KEY (0 ~ 255)", this.hXorPlusKey); + if (this.hXorPlusKey < 0) this.hXorPlusKey = 0; + else if (this.hXorPlusKey > 255) this.hXorPlusKey = 255; + this.tXorPlusKey = EditorGUILayout.IntField("Tail XOR Plus KEY (0 ~ 255)", this.tXorPlusKey); + if (this.tXorPlusKey < 0) this.tXorPlusKey = 0; + else if (this.tXorPlusKey > 255) this.tXorPlusKey = 255; + this.j1XorPlusKey = EditorGUILayout.IntField("Jump 1 XOR Plus KEY (0 ~ 255)", this.j1XorPlusKey); + if (this.j1XorPlusKey < 0) this.j1XorPlusKey = 0; + else if (this.j1XorPlusKey > 255) this.j1XorPlusKey = 255; + this.j2XorPlusKey = EditorGUILayout.IntField("Jump 2 XOR Plus KEY (0 ~ 255)", this.j2XorPlusKey); + if (this.j2XorPlusKey < 0) this.j2XorPlusKey = 0; + else if (this.j2XorPlusKey > 255) this.j2XorPlusKey = 255; + if (EditorGUI.EndChangeCheck()) this._isDirty = true; + + this._DrawOperateButtonsView(this.cryptogramType); + + EditorGUILayout.EndVertical(); + } + #endregion + #region AES [SerializeField] public string aesKey = "aes_key"; [SerializeField] @@ -236,6 +308,7 @@ private void _DrawAesView() EditorGUILayout.EndVertical(); } + #endregion private void _DrawOperateButtonsView(CryptogramType cryptogramType) { @@ -288,6 +361,18 @@ private void _SaveData(CryptogramType cryptogramType, bool isShowDialog = false) if (isShowDialog) EditorUtility.DisplayDialog("Crytogram Message", "Saved [Head-Tail 2 XOR] Setting.", "OK"); break; + case CryptogramType.HT2XorPlus: + this._setting.hXorPlusKey = (byte)this.hXorPlusKey; + this._setting.tXorPlusKey = (byte)this.tXorPlusKey; + this._setting.j1XorPlusKey = (byte)this.j1XorPlusKey; + this._setting.j2XorPlusKey = (byte)this.j2XorPlusKey; + + this._isDirty = false; + EditorUtility.SetDirty(this._setting); + AssetDatabase.SaveAssets(); + + if (isShowDialog) EditorUtility.DisplayDialog("Crytogram Message", "Saved [Head-Tail 2 XOR Plus] Setting.", "OK"); + break; case CryptogramType.Aes: if (string.IsNullOrEmpty(this.aesKey) || string.IsNullOrEmpty(this.aesIv)) { diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/YooAssets/EncryptionServices.cs b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/YooAssets/EncryptionServices.cs index 53da473b..87e02115 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/YooAssets/EncryptionServices.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Editor/Bundle/YooAssets/EncryptionServices.cs @@ -109,6 +109,39 @@ public EncryptResult Encrypt(EncryptFileInfo fileInfo) } } + public class HT2XorPlusEncryption : IEncryptionServices + { + public EncryptResult Encrypt(EncryptFileInfo fileInfo) + { + var cryptogramSettings = CryptogramSettingSetup.GetCryptogramSetting(); + + string filePath = fileInfo.FilePath; + + byte hXorKey = cryptogramSettings.hXorPlusKey; + byte tXorKey = cryptogramSettings.tXorPlusKey; + byte j1XorKey = cryptogramSettings.j1XorPlusKey; + byte j2XorKey = cryptogramSettings.j2XorPlusKey; + + byte[] fileData = File.ReadAllBytes(filePath); + + if (FileCryptogram.HT2XORPlus.HT2XorPlusEncryptBytes(fileData, hXorKey, tXorKey, j1XorKey, j2XorKey)) + { + Debug.Log($"HT2XorPlusCryptogram => hXorKey: {hXorKey}, tXorKey: {tXorKey}, j1XorKey: {j1XorKey}, j2XorKey: {j2XorKey}"); + + EncryptResult result = new EncryptResult(); + result.Encrypted = true; + result.EncryptedData = fileData; + return result; + } + else + { + EncryptResult result = new EncryptResult(); + result.Encrypted = false; + return result; + } + } + } + public class AesEncryption : IEncryptionServices { public EncryptResult Encrypt(EncryptFileInfo fileInfo) diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleConfig.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleConfig.cs index 1f7f96aa..39980ed9 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleConfig.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleConfig.cs @@ -52,6 +52,7 @@ public class CryptogramType public const string OFFSET = "OFFSET"; public const string XOR = "XOR"; public const string HT2XOR = "HT2XOR"; + public const string HT2XORPLUS = "HT2XORPLUS"; public const string AES = "AES"; } diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleInfos.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleInfos.cs index cafbca83..a42b1907 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleInfos.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/BundleInfos.cs @@ -10,7 +10,7 @@ namespace OxGFrame.AssetLoader.Bundle [Serializable] public class DecryptInfo : IDisposable { - [SerializeField, Tooltip("Bundle decryption (case-insensitive).\n\n[NONE], \n[OFFSET, dummySize], \n[XOR, key], \n[HT2XOR, headKey, tailKey, jumpKey], \n[AES, key, iv]\n\nex: \n\"none\" \n\"offset, 12\" \n\"xor, 23\" \n\"ht2xor, 34, 45, 56\" \n\"aes, key, iv\"")] + [SerializeField, Tooltip("Bundle decryption (case-insensitive).\n\n[NONE], \n[OFFSET, dummySize], \n[XOR, key], \n[HT2XOR, headKey, tailKey, jumpKey], \n[HT2XORPLUS, headKey, tailKey, jump1Key, jump2Key], \n[AES, key, iv]\n\nex: \n\"none\" \n\"offset, 12\" \n\"xor, 23\" \n\"ht2xor, 34, 45, 56\" \n\"ht2xorplus, 34, 45, 56, 78\" \n\"aes, key, iv\"")] private string _decryptArgs = BundleConfig.CryptogramType.NONE; [SerializeField, Tooltip("Can encrypt string data in memory.")] public bool secureString = true; diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/FileCryptogram.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/FileCryptogram.cs index c1a5df7a..a61a5db1 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/FileCryptogram.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/FileCryptogram.cs @@ -266,14 +266,15 @@ public static bool HT2XorEncryptFile(string sourceFile, byte hKey, byte tKey, by try { byte[] dataBytes = File.ReadAllBytes(sourceFile); + int length = dataBytes.Length; // head encrypt dataBytes[0] ^= hKey; // tail encrypt - dataBytes[dataBytes.Length - 1] ^= tKey; + dataBytes[length - 1] ^= tKey; // jump 2 encrypt - for (int i = 0; i < dataBytes.Length >> 1; i++) + for (int i = 0; i < length >> 1; i++) { dataBytes[i << 1] ^= jKey; } @@ -298,9 +299,10 @@ public static bool HT2XorDecryptFile(string encryptFile, byte hKey, byte tKey, b try { byte[] dataBytes = File.ReadAllBytes(encryptFile); + int length = dataBytes.Length; // jump 2 encrypt - for (int i = 0; i < dataBytes.Length >> 1; i++) + for (int i = 0; i < length >> 1; i++) { dataBytes[i << 1] ^= jKey; } @@ -308,7 +310,7 @@ public static bool HT2XorDecryptFile(string encryptFile, byte hKey, byte tKey, b // head encrypt dataBytes[0] ^= hKey; // tail encrypt - dataBytes[dataBytes.Length - 1] ^= tKey; + dataBytes[length - 1] ^= tKey; File.WriteAllBytes(encryptFile, dataBytes); } @@ -330,13 +332,15 @@ public static bool HT2XorEncryptBytes(byte[] rawBytes, byte hKey, byte tKey, byt { try { + int length = rawBytes.Length; + // head encrypt rawBytes[0] ^= hKey; // tail encrypt - rawBytes[rawBytes.Length - 1] ^= tKey; + rawBytes[length - 1] ^= tKey; // jump 2 encrypt - for (int i = 0; i < rawBytes.Length >> 1; i++) + for (int i = 0; i < length >> 1; i++) { rawBytes[i << 1] ^= jKey; } @@ -356,8 +360,10 @@ public static bool HT2XorEncryptBytes(byte[] rawBytes, byte hKey, byte tKey, byt /// public static bool HT2XorDecryptBytes(byte[] encryptBytes, byte hKey, byte tKey, byte jKey) { + int length = encryptBytes.Length; + // jump 2 encrypt - for (int i = 0; i < encryptBytes.Length >> 1; i++) + for (int i = 0; i < length >> 1; i++) { encryptBytes[i << 1] ^= jKey; } @@ -365,7 +371,7 @@ public static bool HT2XorDecryptBytes(byte[] encryptBytes, byte hKey, byte tKey, // head encrypt encryptBytes[0] ^= hKey; // tail encrypt - encryptBytes[encryptBytes.Length - 1] ^= tKey; + encryptBytes[length - 1] ^= tKey; return true; } @@ -379,13 +385,14 @@ public static Stream HT2XorDecryptStream(string encryptFile, byte hKey, byte tKe { var fsDecrypt = new FileStream(encryptFile, FileMode.Open, FileAccess.Read, FileShare.None); var dataBytes = new byte[fsDecrypt.Length]; - fsDecrypt.Read(dataBytes, 0, dataBytes.Length); + int length = dataBytes.Length; + fsDecrypt.Read(dataBytes, 0, length); fsDecrypt.Dispose(); var msDecrypt = new MemoryStream(); // jump 2 encrypt - for (int i = 0; i < dataBytes.Length >> 1; i++) + for (int i = 0; i < length >> 1; i++) { dataBytes[i << 1] ^= jKey; } @@ -393,9 +400,185 @@ public static Stream HT2XorDecryptStream(string encryptFile, byte hKey, byte tKe // head encrypt dataBytes[0] ^= hKey; // tail encrypt - dataBytes[dataBytes.Length - 1] ^= tKey; + dataBytes[length - 1] ^= tKey; - msDecrypt.Write(dataBytes, 0, dataBytes.Length); + msDecrypt.Write(dataBytes, 0, length); + + return msDecrypt; + } + } + + public class HT2XORPlus + { + public class WriteFile + { + /// + /// Head-Tail 2 XOR Plus 加密檔案 【檢測OK】 + /// + /// + /// + public static bool HT2XorPlusEncryptFile(string sourceFile, byte hKey, byte tKey, byte j1Key, byte j2Key) + { + try + { + byte[] dataBytes = File.ReadAllBytes(sourceFile); + int length = dataBytes.Length; + + // head encrypt + dataBytes[0] ^= hKey; + // tail encrypt + dataBytes[length - 1] ^= tKey; + + // jump 2 encrypt + for (int i = 0; i < length >> 1; i++) + { + int s1 = i << 1; + int s2 = s1 + 1; + dataBytes[s1] ^= j1Key; + if (s2 < length) + dataBytes[s2] ^= j2Key; + } + + File.WriteAllBytes(sourceFile, dataBytes); + } + catch + { + return false; + } + + return true; + } + + /// + /// Head-Tail 2 XOR Plus 解密檔案 【檢測OK】 + /// + /// + /// + public static bool HT2XorPlusDecryptFile(string encryptFile, byte hKey, byte tKey, byte j1Key, byte j2Key) + { + try + { + byte[] dataBytes = File.ReadAllBytes(encryptFile); + int length = dataBytes.Length; + + // jump 2 plus decrypt + for (int i = 0; i < length >> 1; i++) + { + int s1 = i << 1; + int s2 = s1 + 1; + dataBytes[s1] ^= j1Key; + if (s2 < length) + dataBytes[s2] ^= j2Key; + } + + // head decrypt + dataBytes[0] ^= hKey; + // tail decrypt + dataBytes[length - 1] ^= tKey; + + File.WriteAllBytes(encryptFile, dataBytes); + } + catch + { + return false; + } + + return true; + } + } + + /// + /// Head-Tail 2 XOR Plus 加密檔案 【檢測OK】 + /// + /// + /// + public static bool HT2XorPlusEncryptBytes(byte[] rawBytes, byte hKey, byte tKey, byte j1Key, byte j2Key) + { + try + { + int length = rawBytes.Length; + + // head encrypt + rawBytes[0] ^= hKey; + // tail encrypt + rawBytes[length - 1] ^= tKey; + + // jump 2 plus encrypt + for (int i = 0; i < length >> 1; i++) + { + int s1 = i << 1; + int s2 = s1 + 1; + rawBytes[s1] ^= j1Key; + if (s2 < length) + rawBytes[s2] ^= j2Key; + } + } + catch + { + return false; + } + + return true; + } + + /// + /// Head-Tail 2 XOR Plus 解密檔案 【檢測OK】 + /// + /// + /// + public static bool HT2XorPlusDecryptBytes(byte[] encryptBytes, byte hKey, byte tKey, byte j1Key, byte j2Key) + { + int length = encryptBytes.Length; + + // jump 2 plus decrypt + for (int i = 0; i < length >> 1; i++) + { + int s1 = i << 1; + int s2 = s1 + 1; + encryptBytes[s1] ^= j1Key; + if (s2 < length) + encryptBytes[s2] ^= j2Key; + } + + // head decrypt + encryptBytes[0] ^= hKey; + // tail decrypt + encryptBytes[length - 1] ^= tKey; + + return true; + } + + /// + /// 返回 Head-Tail 2 XOR Plus 解密 Stream 【檢測OK】 + /// + /// + /// + public static Stream HT2XorPlusDecryptStream(string encryptFile, byte hKey, byte tKey, byte j1Key, byte j2Key) + { + var fsDecrypt = new FileStream(encryptFile, FileMode.Open, FileAccess.Read, FileShare.None); + var dataBytes = new byte[fsDecrypt.Length]; + int length = dataBytes.Length; + fsDecrypt.Read(dataBytes, 0, length); + fsDecrypt.Dispose(); + + var msDecrypt = new MemoryStream(); + + // jump 2 plus decrypt + for (int i = 0; i < length >> 1; i++) + { + int s1 = i << 1; + int s2 = s1 + 1; + dataBytes[s1] ^= j1Key; + if (s2 < length) + dataBytes[s2] ^= j2Key; + } + + // head decrypt + dataBytes[0] ^= hKey; + // tail decrypt + dataBytes[length - 1] ^= tKey; + + msDecrypt.Write(dataBytes, 0, length); return msDecrypt; } diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/PackageManager.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/PackageManager.cs index b386c97a..5f6f6a24 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/PackageManager.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/PackageManager.cs @@ -43,6 +43,9 @@ public async static UniTask InitSetup() case BundleConfig.CryptogramType.HT2XOR: _decryption = new HT2XorDecryption(); break; + case BundleConfig.CryptogramType.HT2XORPLUS: + _decryption = new HT2XorPlusDecryption(); + break; case BundleConfig.CryptogramType.AES: _decryption = new AesDecryption(); break; diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/YooAssets/DecryptionServices.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/YooAssets/DecryptionServices.cs index 595f41f8..0d0b2c34 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/YooAssets/DecryptionServices.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Bundle/YooAssets/DecryptionServices.cs @@ -5,12 +5,12 @@ namespace OxGFrame.AssetLoader.Bundle { - internal interface IDecryptStream + public interface IDecryptStream { Stream DecryptStream(DecryptFileInfo fileInfo); } - internal interface IDecryptData + public interface IDecryptData { byte[] DecryptData(DecryptFileInfo fileInfo); } @@ -57,6 +57,7 @@ public byte[] LoadRawFileData(DecryptFileInfo fileInfo) } } + #region Offset public class OffsetDecryption : IDecryptionServices, IDecryptStream, IDecryptData { #region OxGFrame Implements @@ -104,7 +105,9 @@ public byte[] LoadRawFileData(DecryptFileInfo fileInfo) return this.DecryptData(fileInfo); } } + #endregion + #region Xor public class XorDecryption : IDecryptionServices, IDecryptStream, IDecryptData { #region OxGFrame Implements @@ -152,7 +155,9 @@ public byte[] LoadRawFileData(DecryptFileInfo fileInfo) return this.DecryptData(fileInfo); } } + #endregion + #region HT2Xor public class HT2XorDecryption : IDecryptionServices, IDecryptStream, IDecryptData { #region OxGFrame Implements @@ -204,7 +209,65 @@ public byte[] LoadRawFileData(DecryptFileInfo fileInfo) return this.DecryptData(fileInfo); } } + #endregion + #region HT2XorPlus + public class HT2XorPlusDecryption : IDecryptionServices, IDecryptStream, IDecryptData + { + #region OxGFrame Implements + public byte[] DecryptData(DecryptFileInfo fileInfo) + { + OxGFrame.AssetLoader.Utility.SecureMemory.SecureString[] decryptArgs = BundleConfig.decryptArgs; + string filePath = fileInfo.FileLoadPath; + byte hXorkey = Convert.ToByte(decryptArgs[1].Decrypt()); + byte tXorkey = Convert.ToByte(decryptArgs[2].Decrypt()); + byte j1XorKey = Convert.ToByte(decryptArgs[3].Decrypt()); + byte j2XorKey = Convert.ToByte(decryptArgs[4].Decrypt()); + if (File.Exists(filePath) == false) + return null; + byte[] data = File.ReadAllBytes(filePath); + if (FileCryptogram.HT2XORPlus.HT2XorPlusDecryptBytes(data, hXorkey, tXorkey, j1XorKey, j2XorKey)) + return data; + return null; + } + + public Stream DecryptStream(DecryptFileInfo fileInfo) + { + OxGFrame.AssetLoader.Utility.SecureMemory.SecureString[] decryptArgs = BundleConfig.decryptArgs; + string filePath = fileInfo.FileLoadPath; + byte hXorkey = Convert.ToByte(decryptArgs[1].Decrypt()); + byte tXorkey = Convert.ToByte(decryptArgs[2].Decrypt()); + byte j1XorKey = Convert.ToByte(decryptArgs[3].Decrypt()); + byte j2XorKey = Convert.ToByte(decryptArgs[4].Decrypt()); + return FileCryptogram.HT2XORPlus.HT2XorPlusDecryptStream(filePath, hXorkey, tXorkey, j1XorKey, j2XorKey); + } + #endregion + + public uint GetManagedReadBufferSize() + { + return 1024; + } + + public AssetBundle LoadAssetBundle(DecryptFileInfo fileInfo, out Stream managedStream) + { + managedStream = this.DecryptStream(fileInfo); + return AssetBundle.LoadFromStream(managedStream, fileInfo.ConentCRC, GetManagedReadBufferSize()); + } + + public AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream) + { + managedStream = this.DecryptStream(fileInfo); + return AssetBundle.LoadFromStreamAsync(managedStream, fileInfo.ConentCRC, GetManagedReadBufferSize()); + } + + public byte[] LoadRawFileData(DecryptFileInfo fileInfo) + { + return this.DecryptData(fileInfo); + } + } + #endregion + + #region AES public class AesDecryption : IDecryptionServices, IDecryptStream, IDecryptData { #region OxGFrame Implements @@ -254,4 +317,5 @@ public byte[] LoadRawFileData(DecryptFileInfo fileInfo) return this.DecryptData(fileInfo); } } + #endregion } \ No newline at end of file diff --git a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Utility/CryptogramUtility.cs b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Utility/CryptogramUtility.cs index 6b277271..fd634633 100644 --- a/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Utility/CryptogramUtility.cs +++ b/Assets/OxGFrame/AssetLoader/Scripts/Runtime/Utility/CryptogramUtility.cs @@ -5,6 +5,7 @@ namespace OxGFrame.AssetLoader.Utility { public class CryptogramUtility { + #region AES public static void AesEncryptBundleFiles(string dir, string key = null, string iv = null) { // 取得目錄下所有檔案 @@ -32,7 +33,9 @@ public static void AesDecryptBundleFiles(string dir, string key = null, string i FileCryptogram.AES.WriteFile.AesDecryptFile(fPath, key, iv); } } + #endregion + #region Xor public static void XorEncryptBundleFiles(string dir, byte key) { // 取得目錄下所有檔案 @@ -60,7 +63,9 @@ public static void XorDecryptBundleFiles(string dir, byte key) FileCryptogram.XOR.WriteFile.XorDecryptFile(fPath, key); } } + #endregion + #region HT2Xor public static void HT2XorEncryptBundleFiles(string dir, byte hKey, byte tKey, byte jKey) { // 取得目錄下所有檔案 @@ -88,7 +93,39 @@ public static void HT2XorDecryptBundleFiles(string dir, byte hKey, byte tKey, by FileCryptogram.HT2XOR.WriteFile.HT2XorDecryptFile(fPath, hKey, tKey, jKey); } } + #endregion + #region HT2XorPlus + public static void HT2XorPlusEncryptBundleFiles(string dir, byte hKey, byte tKey, byte j1Key, byte j2key) + { + // 取得目錄下所有檔案 + FileInfo[] files = BundleUtility.GetFilesRecursively(dir); + + // 對所有檔案進行加密 + for (int i = 0; i < files.Length; i++) + { + // 執行各檔案的加密 + string fPath = Path.Combine(files[i].Directory.ToString(), files[i].Name); + FileCryptogram.HT2XORPlus.WriteFile.HT2XorPlusEncryptFile(fPath, hKey, tKey, j1Key, j2key); + } + } + + public static void HT2XorPlusDecryptBundleFiles(string dir, byte hKey, byte tKey, byte j1Key, byte j2key) + { + // 取得目錄下所有檔案 + FileInfo[] files = BundleUtility.GetFilesRecursively(dir); + + // 對所有檔案進行解密 + for (int i = 0; i < files.Length; i++) + { + // 執行各檔案的解密 + string fPath = Path.Combine(files[i].Directory.ToString(), files[i].Name); + FileCryptogram.HT2XORPlus.WriteFile.HT2XorPlusDecryptFile(fPath, hKey, tKey, j1Key, j2key); + } + } + #endregion + + #region Offset public static void OffsetEncryptBundleFiles(string dir, int randomSeed, int dummySize = 0) { // 取得目錄下所有檔案 @@ -116,5 +153,6 @@ public static void OffsetDecryptBundleFiles(string dir, int dummySize = 0) FileCryptogram.Offset.WriteFile.OffsetDecryptFile(fPath, dummySize); } } + #endregion } } \ No newline at end of file diff --git a/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/CHANGELOG.md b/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/CHANGELOG.md index c6cd07e8..0c56e09b 100644 --- a/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/CHANGELOG.md +++ b/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/CHANGELOG.md @@ -2,6 +2,42 @@ All notable changes to this package will be documented in this file. +## [2.1.2] - 2024-05-16 + +SBP库依赖版本升级至2.1.3 + +### Fixed + +- (#236) 修复了资源配置界面AutoCollectShader复选框没有刷新的问题。 +- (#244) 修复了导入器在安卓平台导入本地下载的资源失败的问题。 +- (#268) 修复了挂起场景未解除状态前无法卸载的问题。 +- (#269) 优化场景挂起流程,支持中途取消挂起操作。 +- (#276) 修复了HostPlayMode模式下,如果内置清单是最新版本,每次运行都会触发拷贝行为。 +- (#289) 修复了Unity2019版本脚本IWebRequester编译报错。 +- (#295) 解决了在安卓移动平台,华为和三星真机上有极小概率加载资源包失败 : Unable to open archive file + +### Added + +- 新增GetAllCacheFileInfosOperation()获取缓存文件信息的方法。 + +- 新增LoadSceneSync()同步加载场景的方法。 + +- 新增IIgnoreRule接口,资源收集流程可以自定义。 + +- 新增IWechatQueryServices接口,用于微信平台本地文件查询。 + + 后续将会通过虚拟文件系统来支持! + +### Changed + +- 调整了UnloadSceneOperation代码里场景的卸载顺序。 + +### Improvements + +- 优化了资源清单的解析过程。 +- 移除资源包名里的空格字符。 +- 支持华为鸿蒙系统。 + ## [2.1.1] - 2024-01-17 ### Fixed diff --git a/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/package.json b/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/package.json index 6863ffcf..da3c70c2 100644 --- a/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/package.json +++ b/Assets/OxGFrame/AssetLoader/ThirdParty/YooAsset/package.json @@ -1,7 +1,7 @@ { "name": "com.tuyoogame.yooasset", "displayName": "YooAsset", - "version": "2.1.1", + "version": "2.1.2", "unity": "2019.4", "description": "unity3d resources management system.", "author": { diff --git a/Assets/OxGFrame/CHANGELOG.md b/Assets/OxGFrame/CHANGELOG.md index 2c0e3dd8..714c9ee1 100644 --- a/Assets/OxGFrame/CHANGELOG.md +++ b/Assets/OxGFrame/CHANGELOG.md @@ -1,23 +1,27 @@ # CHANGELOG -## [2.11.0] -2024-05-12 +## [2.11.1] - 2024-05-17 +- Added HT2XORPlus encryption stronger than HT2XOR (Recommended). +- Updated YooAsset to v2.1.2 (new commits). + +## [2.11.0] - 2024-05-12 - Updated YooAsset commits. - Updated Scriptable Build Pipeline to v2.1.3 (Unity). - Added YooAsset can support RawFile encryption. - Added Hotfixer can new PackageInfoWithBuild to CheckHotfix method. - Optimized Support Hotfix to be loaded using other threads (WebGL is not supported). -## [2.10.5] -2024-04-17 +## [2.10.5] - 2024-04-17 - Fixed SimpleDiskUtils compile error issue on WIN. -## [2.10.4] -2024-04-08 +## [2.10.4] - 2024-04-08 - Removed UniTask from built-in. - Please use install via git URL instead (Package Manager) ``` https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask ``` -## [2.10.3] -2024-04-01 +## [2.10.3] - 2024-04-01 - Added CacheType to AudioManager, which will be used for caching when AudioBase is using the request method. - Modified AudioBase to not rely on the methods of the OxGKit.Utilities' Requester. - Modified Acax encoding type (use UTF-8). diff --git a/Assets/OxGFrame/package.json b/Assets/OxGFrame/package.json index 45c5e0af..cbf1899a 100644 --- a/Assets/OxGFrame/package.json +++ b/Assets/OxGFrame/package.json @@ -2,7 +2,7 @@ "name": "com.michaelo.oxgframe", "displayName": "OxGFrame", "description": "The OxGFrame is a framework based on Unity for accelerating game development. Supports multi-platform Win, OSX, Android, iOS, WebGL.", - "version": "2.11.0", + "version": "2.11.1", "unity": "2021.3", "license": "MIT", "samples": [ diff --git a/Assets/Settings/OxGFrame/CryptogramSetting.asset b/Assets/Settings/OxGFrame/CryptogramSetting.asset index 046b8529..a8a10d1d 100644 --- a/Assets/Settings/OxGFrame/CryptogramSetting.asset +++ b/Assets/Settings/OxGFrame/CryptogramSetting.asset @@ -15,8 +15,12 @@ MonoBehaviour: randomSeed: 1 dummySize: 1 xorKey: 1 - hXorKey: 1 - tXorKey: 1 - jXorKey: 1 + hXorKey: 5 + tXorKey: 6 + jXorKey: 7 + hXorPlusKey: 10 + tXorPlusKey: 20 + j1XorPlusKey: 55 + j2XorPlusKey: 11 aesKey: aes_key aesIv: aes_iv diff --git a/README.md b/README.md index baedec8c..9142122e 100644 --- a/README.md +++ b/README.md @@ -87,10 +87,10 @@ https://github.com/michael811125/OxGFrame/assets/30960759/fd04f6e5-6338-400c-9f5 **選擇使用 Bundle 開發時,需要先將 PatchLauncher 拖曳至場景中,才能驅動相關配置。【如果使用 PakcageManager 安裝的,透過 Samples Import PatchLauncher Prefab】** - FileCryptogram (檔案加解密) - - 運算效率 OFFSET > HT2XOR > XOR > AES - - 內存占用 OFFSET > AES > HT2XOR = XOR - - AB 包體積增加 OFFSET > AES > HT2XOR = XOR - - 破解難度 AES > HT2XOR > XOR > OFFSET + - 運算效率 OFFSET > HT2XOR >= HT2XORPlus > XOR > AES + - 內存占用 OFFSET > AES > HT2XORPlus = HT2XOR = XOR + - AB 包體積增加 OFFSET > AES > HT2XORPlus = HT2XOR = XOR + - 破解難度 AES > HT2XORPlus > HT2XOR > XOR > OFFSET #### 群組分包舉例 - 最小運行包