Skip to content

Commit

Permalink
added HT2Xor and deprecated HTXor
Browse files Browse the repository at this point in the history
  • Loading branch information
michael811125 committed Jul 7, 2023
1 parent 8a6e951 commit 4130a1c
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Assets/AssetBundleBuilderSetting.asset
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ MonoBehaviour:
BuildPackage: DefaultPackage
CompressOption: 2
OutputNameStyle: 1
CopyBuildinFileOption: 1
CopyBuildinFileOption: 0
CopyBuildinFileTags:
EncyptionClassName: XorEncryption
EncyptionClassName: HT2XorEncryption
1 change: 1 addition & 0 deletions Assets/CryptogramSetting.asset
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ MonoBehaviour:
xorKey: 1
hXorKey: 1
tXorKey: 1
jXorKey: 1
aesKey: aes_key
aesIv: aes_iv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OxGFrame.AssetLoader.Editor
{
[CreateAssetMenu(fileName = nameof(CryptogramSetting), menuName = "OxGFrame/Create Settings/Cryptograme Setting")]
[CreateAssetMenu(fileName = nameof(CryptogramSetting), menuName = "OxGFrame/Create Settings/Cryptogram Setting")]
public class CryptogramSetting : ScriptableObject
{
[Header("OFFSET")]
Expand All @@ -12,9 +12,10 @@ public class CryptogramSetting : ScriptableObject
[Header("XOR")]
public byte xorKey = 1;

[Header("HTXOR")]
[Header("HT2XOR")]
public byte hXorKey = 1;
public byte tXorKey = 1;
public byte jXorKey = 1;

[Header("AES")]
public string aesKey = "aes_key";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
using OxGFrame.AssetLoader.Utility;
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using YooAsset.Editor;

namespace OxGFrame.AssetLoader.Editor
{
public class BundleCryptogramUtilityWindow : EditorWindow
{
public enum CryptogramType
{
OFFSET,
XOR,
HT2XOR,
AES
}

private static BundleCryptogramUtilityWindow _instance = null;
internal static BundleCryptogramUtilityWindow GetInstance()
{
if (_instance == null) _instance = GetWindow<BundleCryptogramUtilityWindow>();
return _instance;
}

[SerializeField]
public CryptogramType cryptogramType;

[SerializeField]
public string sourceFolder;

private CryptogramSetting _settings;

internal static string PROJECT_PATH;
internal static string KEY_SAVER;

private static Vector2 _windowSize = new Vector2(800f, 150f);

[MenuItem(BundleHelper.MenuRoot + "Bundle Cryptogram Utility (For Verify)", false, 699)]
public static void ShowWindow()
{
PROJECT_PATH = Application.dataPath;
KEY_SAVER = $"{PROJECT_PATH}_{nameof(BundleCryptogramUtilityWindow)}";

_instance = null;
GetInstance().titleContent = new GUIContent("Bundle Cryptogram Utility");
GetInstance().Show();
GetInstance().minSize = _windowSize;
}

private void OnEnable()
{
this._settings = EditorTool.LoadSettingData<CryptogramSetting>();
this._LoadSettingsData();
this.sourceFolder = EditorStorage.GetData(KEY_SAVER, $"sourceFolder", Path.Combine($"{Application.dataPath}/", AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()));
this.cryptogramType = (CryptogramType)Convert.ToInt32(EditorStorage.GetData(KEY_SAVER, "cryptogramType", "0"));
}

private void _LoadSettingsData()
{
this.randomSeed = this._settings.randomSeed;
this.dummySize = this._settings.dummySize;
this.xorKey = this._settings.xorKey;
this.hXorKey = this._settings.hXorKey;
this.tXorKey = this._settings.tXorKey;
this.jXorKey = this._settings.jXorKey;
this.aesKey = this._settings.aesKey;
this.aesIv = this._settings.aesIv;
}

private void OnDisable()
{
base.SaveChanges();
}

private void OnGUI()
{
EditorGUILayout.BeginHorizontal();
EditorGUI.BeginChangeCheck();
this.sourceFolder = EditorGUILayout.TextField("Source Folder", this.sourceFolder);
if (EditorGUI.EndChangeCheck()) EditorStorage.SaveData(KEY_SAVER, "sourceFolder", this.sourceFolder);
Color bc = GUI.backgroundColor;
GUI.backgroundColor = new Color32(0, 255, 128, 255);
if (GUILayout.Button("Open", GUILayout.MaxWidth(100f))) BundleUtility.OpenFolder(this.sourceFolder);
GUI.backgroundColor = bc;
bc = GUI.backgroundColor;
GUI.backgroundColor = new Color32(83, 152, 255, 255);
if (GUILayout.Button("Browse", GUILayout.MaxWidth(100f))) this._OpenSourceFolder();
GUI.backgroundColor = bc;
EditorGUILayout.EndHorizontal();

EditorGUILayout.Space();

EditorGUILayout.BeginHorizontal();

// cryptogram options area
EditorGUI.BeginChangeCheck();
this.cryptogramType = (CryptogramType)EditorGUILayout.EnumPopup("Cryptogram Type", this.cryptogramType);
if (EditorGUI.EndChangeCheck()) EditorStorage.SaveData(KEY_SAVER, "cryptogramType", ((int)this.cryptogramType).ToString());

EditorGUILayout.EndHorizontal();

this._CryptogramType(this.cryptogramType);
}

private void _CryptogramType(CryptogramType cryptogramType)
{
switch (cryptogramType)
{
case CryptogramType.OFFSET:
this._DrawOffsetView();
break;
case CryptogramType.XOR:
this._DrawXorView();
break;
case CryptogramType.HT2XOR:
this._DrawHT2XorView();
break;
case CryptogramType.AES:
this._DrawAesView();
break;
}
}

[SerializeField]
public int randomSeed = 1;
[SerializeField]
public int dummySize = 0;
private void _DrawOffsetView()
{
EditorGUILayout.Space();

GUIStyle style = new GUIStyle();
var bg = new Texture2D(1, 1);
ColorUtility.TryParseHtmlString("#1c589c", 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("Offset Settings"), centeredStyle);
EditorGUILayout.Space();

this.randomSeed = EditorGUILayout.IntField(new GUIContent("Random Seed", "Fixed random values."), this.randomSeed);
if (this.randomSeed <= 0) this.randomSeed = 1;

this.dummySize = EditorGUILayout.IntField(new GUIContent("Offset Dummy Size", "Add dummy bytes into front of file (per byte = Random 0 ~ 255)."), this.dummySize);
if (this.dummySize < 0) this.dummySize = 0;

this._DrawOperateButtonsView(this.cryptogramType);

EditorGUILayout.EndVertical();
}

[SerializeField]
public int xorKey = 0;
private void _DrawXorView()
{
EditorGUILayout.Space();

GUIStyle style = new GUIStyle();
var bg = new Texture2D(1, 1);
ColorUtility.TryParseHtmlString("#1c589c", 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("XOR Settings"), centeredStyle);
EditorGUILayout.Space();

this.xorKey = EditorGUILayout.IntField("XOR KEY (0 ~ 255)", this.xorKey);
if (this.xorKey < 0) this.xorKey = 0;
else if (this.xorKey > 255) this.xorKey = 255;

this._DrawOperateButtonsView(this.cryptogramType);

EditorGUILayout.EndVertical();
}

[SerializeField]
public int hXorKey = 0;
[SerializeField]
public int tXorKey = 0;
[SerializeField]
public int jXorKey = 0;
private void _DrawHT2XorView()
{
EditorGUILayout.Space();

GUIStyle style = new GUIStyle();
var bg = new Texture2D(1, 1);
ColorUtility.TryParseHtmlString("#1c589c", 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 Settings"), centeredStyle);
EditorGUILayout.Space();

this.hXorKey = EditorGUILayout.IntField("Head XOR KEY (0 ~ 255)", this.hXorKey);
if (this.hXorKey < 0) this.hXorKey = 0;
else if (this.hXorKey > 255) this.hXorKey = 255;
this.tXorKey = EditorGUILayout.IntField("Tail XOR KEY (0 ~ 255)", this.tXorKey);
if (this.tXorKey < 0) this.tXorKey = 0;
else if (this.tXorKey > 255) this.tXorKey = 255;
this.jXorKey = EditorGUILayout.IntField("Jump XOR KEY (0 ~ 255)", this.jXorKey);
if (this.jXorKey < 0) this.jXorKey = 0;
else if (this.jXorKey > 255) this.jXorKey = 255;

this._DrawOperateButtonsView(this.cryptogramType);

EditorGUILayout.EndVertical();
}

[SerializeField]
public string aesKey = "file_key";
[SerializeField]
public string aesIv = "file_iv";
private void _DrawAesView()
{
EditorGUILayout.Space();

GUIStyle style = new GUIStyle();
var bg = new Texture2D(1, 1);
ColorUtility.TryParseHtmlString("#1c589c", 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("AES Settings"), centeredStyle);
EditorGUILayout.Space();

this.aesKey = EditorGUILayout.TextField("AES KEY", this.aesKey);
this.aesIv = EditorGUILayout.TextField("AES IV", this.aesIv);

this._DrawOperateButtonsView(this.cryptogramType);

EditorGUILayout.EndVertical();
}

private void _DrawOperateButtonsView(CryptogramType cryptogramType)
{
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
Color bc = GUI.backgroundColor;
GUI.backgroundColor = new Color32(255, 185, 83, 255);
if (GUILayout.Button("Decrypt", GUILayout.MaxWidth(100f)))
{
switch (cryptogramType)
{
case CryptogramType.OFFSET:
CryptogramUtility.OffsetDecryptBundleFiles(this.sourceFolder, this.dummySize);
EditorUtility.DisplayDialog("Crytogram Message", "[OFFSET] Decrypt Process.", "OK");
break;
case CryptogramType.XOR:
CryptogramUtility.XorDecryptBundleFiles(this.sourceFolder, (byte)this.xorKey);
EditorUtility.DisplayDialog("Crytogram Message", "[XOR] Decrypt Process.", "OK");
break;
case CryptogramType.HT2XOR:
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.AES:
if (string.IsNullOrEmpty(this.aesKey) || string.IsNullOrEmpty(this.aesIv))
{
EditorUtility.DisplayDialog("Crytogram Message", "[AES] KEY or IV is Empty!!! Can't process.", "OK");
break;
}
CryptogramUtility.AesDecryptBundleFiles(this.sourceFolder, this.aesKey, this.aesIv);
EditorUtility.DisplayDialog("Crytogram Message", "[AES] Decrypt Process.", "OK");
break;
}
}
GUI.backgroundColor = bc;

bc = GUI.backgroundColor;
GUI.backgroundColor = new Color32(255, 74, 218, 255);
if (GUILayout.Button("Encrypt", GUILayout.MaxWidth(100f)))
{
switch (cryptogramType)
{
case CryptogramType.OFFSET:
CryptogramUtility.OffsetEncryptBundleFiles(this.sourceFolder, this.randomSeed, this.dummySize);
EditorUtility.DisplayDialog("Crytogram Message", "[OFFSET] Encrypt Process.", "OK");
break;
case CryptogramType.XOR:
CryptogramUtility.XorEncryptBundleFiles(this.sourceFolder, (byte)this.xorKey);
EditorUtility.DisplayDialog("Crytogram Message", "[XOR] Encrypt Process.", "OK");
break;
case CryptogramType.HT2XOR:
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.AES:
if (string.IsNullOrEmpty(this.aesKey) || string.IsNullOrEmpty(this.aesIv))
{
EditorUtility.DisplayDialog("Crytogram Message", "[AES] KEY or IV is Empty!!! Can't process.", "OK");
break;
}
CryptogramUtility.AesEncryptBundleFiles(this.sourceFolder, this.aesKey, this.aesIv);
EditorUtility.DisplayDialog("Crytogram Message", "[AES] Encrypt Process.", "OK");
break;
}
}
GUI.backgroundColor = bc;
EditorGUILayout.EndHorizontal();
}

private void _OpenSourceFolder()
{
string folderPath = EditorStorage.GetData(KEY_SAVER, "sourceFolder", Path.Combine($"{Application.dataPath}/", AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()));
this.sourceFolder = EditorUtility.OpenFolderPanel("Open Source Folder", folderPath, string.Empty);
if (!string.IsNullOrEmpty(this.sourceFolder)) EditorStorage.SaveData(KEY_SAVER, "sourceFolder", this.sourceFolder);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4130a1c

Please sign in to comment.