/// <summary> /// AES 加密 /// </summary> public static byte[] AESEncrypt(byte[] array, EnciphererKey key) { if (array.Length <= 0) { return(null); } RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key.Key), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; if (key.KeyVector.Length == 0) { rm.GenerateIV(); key.KeyVector = rm.IV; } else { rm.IV = key.KeyVector; } MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, rm.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(array, 0, array.Length); cs.FlushFinalBlock(); byte[] cipherBytes = ms.ToArray(); cs.Close(); ms.Close(); return(cipherBytes); }
private void LoadPlatformMainfest(string rootBundlePath, string folerPath, EnciphererKey keyAsset) { //从内存中加载&解密 byte[] datas = Encipherer.AESDecrypt(File.ReadAllBytes(rootBundlePath), keyAsset); AssetBundle mainfestAssetBundle = AssetBundle.LoadFromMemory(datas); _mainfest = mainfestAssetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); // string[] assetBundleNames = _mainfest.GetAllAssetBundles(); foreach (var item in assetBundleNames) { datas = Encipherer.AESDecrypt(File.ReadAllBytes(folerPath + "/" + item), keyAsset); AssetBundle assetBundle = AssetBundle.LoadFromMemory(datas); string[] assetNames = assetBundle.GetAllAssetNames(); if (assetBundle.isStreamedSceneAssetBundle) { assetNames = assetBundle.GetAllScenePaths(); } foreach (var name in assetNames) { if (!_allAssets.ContainsKey(name)) { _allAssets.Add(name, assetBundle); } } } mainfestAssetBundle.Unload(false); }
/// <summary> /// 设置资源的路径,默认是为只读路径:Application.streamingAssetsPath; /// </summary> /// <param name="path"></param> public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles", bool isEncrypt = false) { switch (pathType) { case PathType.ReadOnly: _readPath = Application.streamingAssetsPath; break; case PathType.ReadWrite: _readPath = Application.persistentDataPath; break; case PathType.DataPath: _readPath = Application.dataPath; break; case PathType.TemporaryCache: _readPath = Application.temporaryCachePath; break; default: _readPath = Application.streamingAssetsPath; break; } string rootABPath = _readPath + "/" + rootAssetBundle; string directionPath = _readPath; int index = rootAssetBundle.LastIndexOf("/"); if (index > 0 && index < (rootAssetBundle.Length - 1)) { directionPath += "/" + rootAssetBundle.Substring(0, index); } //加载mainfest文件 if (!isEncrypt) { LoadPlatformMainfest(rootABPath, directionPath); } else { EnciphererKey keyAsset = Resources.Load("Key") as EnciphererKey; LoadPlatformMainfest(rootABPath, directionPath, keyAsset); } }
/// <summary> /// 设置资源的路径,默认是为只读路径:Application.streamingAssetsPath; /// </summary> /// <param name="path"></param> public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles", bool isEncrypt = false) { switch (pathType) { case PathType.ReadOnly: _readPath = Application.streamingAssetsPath; break; case PathType.ReadWrite: _readPath = Application.persistentDataPath; break; case PathType.DataPath: _readPath = Application.dataPath; break; case PathType.TemporaryCache: _readPath = Application.temporaryCachePath; break; default: _readPath = Application.streamingAssetsPath; break; } string rootAbPath = Path.Combine(_readPath, rootAssetBundle); _readPath = Path.GetDirectoryName(rootAbPath); //加载mainfest文件 if (isEncrypt) { _enciphererkeyAsset = Resources.Load("Key") as EnciphererKey; if (_enciphererkeyAsset == null) { throw new GamekException("Resource EnciphererKey not Found"); } } LoadPlatformMainfest(rootAbPath); }
/// <summary> /// AES 解密 /// </summary> public static string AesDecrypt(string str, EnciphererKey key) { if (string.IsNullOrEmpty(str)) { return(null); } byte[] toEncryptArray = Convert.FromBase64String(str); RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key.Key), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform = rm.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Encoding.UTF8.GetString(resultArray)); }
public static void BuildAssetBundles() { string buildPath = EditorConfigInfo.BuildPath; if (!Directory.Exists(buildPath)) { Debug.LogError("Please set build path!"); return; } //打包资源 Debug.Log("开始打包!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode; BuildTarget target = (BuildTarget)EditorConfigInfo.BuildTarget; BuildPipeline.BuildAssetBundles(buildPath, option, target); Debug.Log("打包完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); //资源加密 if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES) { string keyPath = Application.dataPath + "/Resources"; if (!Directory.Exists(keyPath)) { Directory.CreateDirectory(keyPath); } if (!File.Exists(keyPath + "/Key.asset")) { EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>(); ek.GeneraterKey(); AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset"); } EnciphererKey keyAsset = Resources.Load("Key") as EnciphererKey; DirectoryInfo dir = new DirectoryInfo(buildPath); FileSystemInfo[] infos = dir.GetFileSystemInfos(); Debug.Log("开始加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); foreach (FileSystemInfo info in infos) { if (info is FileInfo) { if (info.Extension == "") { byte[] bs = File.ReadAllBytes(info.FullName); byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset); File.WriteAllBytes(info.FullName, cipbs); Debug.Log("完成资源包 " + info.Name + " 的加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); } } } Debug.Log("加密完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); } //写入版本号信息 string assetVersionPath = buildPath + "/AssetVersion.txt"; AssetBundleVersionInfo version = new AssetBundleVersionInfo(); version.Version = EditorConfigInfo.AssetVersion; version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES; version.Resources = new List <ResourcesInfo>(); int index = buildPath.LastIndexOf("/", StringComparison.Ordinal); if (index > 0) { version.ManifestAssetBundle = buildPath.Substring(index + 1, buildPath.Length - index - 1); } DirectoryInfo dir1 = new DirectoryInfo(buildPath); FileSystemInfo[] infos1 = dir1.GetFileSystemInfos(); foreach (FileSystemInfo info in infos1) { if (info is FileInfo) { if (info.Extension == "") { ResourcesInfo ri = new ResourcesInfo(); ri.Name = info.Name; ri.Hash = info.GetHashCode().ToString(); version.Resources.Add(ri); } } } string content = JsonUtility.ToJson(version); File.WriteAllText(assetVersionPath, content); //版本号迭代 EditorConfigInfo.AssetVersion += 1; SaveEditorConfigInfo(); AssetDatabase.Refresh(); //打开打包文件夹 EditorUtility.OpenWithDefaultApp(buildPath); }
public static void BuildAssetBundles() { string buildPath = EditorConfigInfo.BuildPath; if (!Directory.Exists(buildPath)) { Debug.LogError("Please set build path!"); return; } //平台资源的信息 string platformVersionPath = Path.Combine(buildPath, "AssetPlatformVersion.txt"); AssetPlatformVersionInfo assetPlatformVersionInfo = new AssetPlatformVersionInfo(); assetPlatformVersionInfo.Platforms = new List <string>(); //资源加密 EnciphererKey keyAsset = null; if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES) { string keyPath = Application.dataPath + "/Resources"; if (!Directory.Exists(keyPath)) { Directory.CreateDirectory(keyPath); } if (!File.Exists(keyPath + "/Key.asset")) { EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>(); ek.GeneraterKey(); AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset"); AssetDatabase.Refresh(); } keyAsset = Resources.Load("Key") as EnciphererKey; } //根据各个平台打包 foreach (var item in EditorConfigInfo.BuildTargets) { //打包 BuildTarget target = (BuildTarget)item; string targetName = target.ToString().ToLower(); //添加平台的信息 assetPlatformVersionInfo.Platforms.Add(targetName); string targetBuildPath = Path.Combine(buildPath, targetName); if (!Directory.Exists(targetBuildPath)) { Directory.CreateDirectory(targetBuildPath); } Debug.Log("开始打包--" + targetName + System.DateTime.Now.ToString(" HH:mm:ss:fff")); BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode; BuildPipeline.BuildAssetBundles(targetBuildPath, option, target); Debug.Log("打包完成--" + targetName + System.DateTime.Now.ToString(" HH:mm:ss:fff")); //写入版本号信息 string assetVersionPath = targetBuildPath + "/AssetVersion.txt"; AssetBundleVersionInfo version = new AssetBundleVersionInfo(); version.ManifestAssetBundle = targetName; version.Version = EditorConfigInfo.AssetVersion; version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES; version.Resources = new List <ResourcesInfo>(); List <FileInfo> allFiles = GetAllFiles(targetBuildPath); for (int i = 0; i < allFiles.Count; i++) { FileInfo fileInfo = allFiles[i]; if (fileInfo.Extension == ".manifest") { File.Delete(fileInfo.FullName); } else if (fileInfo.Name == "AssetVersion.txt") { continue; } //加密 else { byte[] bs = File.ReadAllBytes(fileInfo.FullName); if (keyAsset != null) { byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset); File.WriteAllBytes(fileInfo.FullName, cipbs); //加密后md5的值应该刷新 bs = cipbs; } ResourcesInfo resourcesInfo = new ResourcesInfo(); string fullPath = Path.GetFullPath(targetBuildPath); resourcesInfo.Name = fileInfo.FullName.Replace(fullPath + "\\", ""); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //计算字节数组的哈希值 byte[] toData = md5.ComputeHash(bs); string fileMD5 = ""; for (int j = 0; j < toData.Length; j++) { fileMD5 += toData[j].ToString("x2"); } resourcesInfo.MD5 = fileMD5; version.Resources.Add(resourcesInfo); } } //保存AssetVersion文件 string content = JsonUtility.ToJson(version); File.WriteAllText(assetVersionPath, content); } //保存平台信息文件 string platformContent = JsonUtility.ToJson(assetPlatformVersionInfo); File.WriteAllText(platformVersionPath, platformContent); //版本号迭代 EditorConfigInfo.AssetVersion += 1; SaveEditorConfigInfo(); AssetDatabase.Refresh(); //打开打包文件夹 EditorUtility.OpenWithDefaultApp(buildPath); }