using UnityEngine; using UnityEditor; using System.IO; using System; namespace BFEditor.Build { public static class ProjectEncryptUtils { const string NHP_PATH = "BFVersions/NHPProtect/NHPProtect.jar"; const string LUA_ENCRYPT_KEY = "reD9xI4k5MR0qqi8"; const string LUA_ENCRYPT_HEAD = "bf_encrypt_lua"; /// /// assetBundle加密 /// public static bool EncryptAssetBundle(string abPath, bool showDialog = false) { Debug.Log("[bfinfo]正在加密assetbundle..."); var success = true; var path = Application.dataPath.Replace("Assets", NHP_PATH); var args = string.Format("-jar {0} -SingleAstEnc {1}", path, abPath); EditorUtility.DisplayProgressBar("提示", "正在加密assetbundle...", 0.5f); BFEditorUtils.RunCommond("java", args, null, (msg) => { Debug.Log(msg); }, (errorMsg) => { Debug.LogError(errorMsg); success = false; } ); EditorUtility.ClearProgressBar(); if (success) { Directory.Delete(abPath, true); Debug.Log("删除未加密AB:" + abPath); var dest = abPath; var src = dest + "_encrypted"; Directory.Move(src, dest); Debug.Log("加密AB路径改名:" + src + "---->" + dest); var tips = "加密AssetBundle成功"; if (showDialog) { EditorUtility.DisplayDialog("提示", tips, "确定"); } else { Debug.Log(tips); } } else { if (showDialog) { EditorUtility.DisplayDialog("提示", "加密AssetBundle失败", "确定"); } else { Debug.LogError("加密AssetBundle失败"); } } return success; } /// /// apk加密 /// public static bool EncryptApk(string apkPath, bool showDialog = false) { var success = true; var path = Application.dataPath.Replace("Assets", NHP_PATH); var args = string.Format("-jar {0} -yunconfig -apksign -zipalign -input ", path) + apkPath; EditorUtility.DisplayProgressBar("提示", "正在加密apk", 0.5f); BFEditorUtils.RunCommond("java", args, null, (msg) => { Debug.Log(msg); }, (errorMsg) => { Debug.LogError(errorMsg); success = false; } ); EditorUtility.ClearProgressBar(); if (success) { var fileName = Path.GetFileNameWithoutExtension(apkPath); var encryptedName = apkPath.Replace(fileName, fileName + "_encrypted"); var tips = "加密APK成功,APK路径:" + encryptedName; if (showDialog) { EditorUtility.DisplayDialog("提示", tips, "确定"); } else { Debug.Log(tips); } } else { if (showDialog) { EditorUtility.DisplayDialog("提示", "加密APK失败", "确定"); } else { Debug.LogError("加密APK失败"); } } return success; } public static void EncryptLua() { Debug.Log("[bfinfo]正在加密lua..."); var luaPath = Path.Combine(Application.dataPath, "lua"); var dirInfo = new DirectoryInfo(luaPath); var srcFiles = dirInfo.GetFiles("*.lua.bytes", SearchOption.AllDirectories); foreach (var file in srcFiles) { var bytes = File.ReadAllBytes(file.FullName); var index = 0; foreach (var item in bytes) { bytes[index] = (byte)(item ^ (LUA_ENCRYPT_KEY[index % LUA_ENCRYPT_KEY.Length])); index++; } var newbytes = new byte[bytes.Length + LUA_ENCRYPT_HEAD.Length]; index = 0; foreach (var item in LUA_ENCRYPT_HEAD) { var headByte = (byte)item; newbytes[index] = headByte; index++; } foreach (var item in bytes) { newbytes[index] = item; index++; } File.WriteAllBytes(file.FullName, newbytes); } } } }