unity2019实现打包ab包头部偏移加密
时间: 2023-09-16 19:11:00 浏览: 121
Unity开发-AES加密解密打包加载资源
5星 · 资源好评率100%
Unity 2019支持使用AES加密算法对Asset Bundle进行加密,可以使用Unity提供的工具进行自动加密。以下是一些步骤:
1. 打开Unity编辑器,选择菜单栏中的“Edit”->“Project Settings”->“Player”,在右侧的Inspector面板中找到“Other Settings”部分。
2. 在“Other Settings”部分中,将“Asset Serialization”选项设置为“Force Text”。
3. 在同一个面板中,找到“Scripting Define Symbols”选项,添加一个自定义的编译符号“USE_ENCRYPTION”。
4. 创建一个名为“EncryptionHelper”的C#脚本,并将以下代码复制到脚本中:
```
#if USE_ENCRYPTION
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Security.Cryptography;
public class EncryptionHelper : AssetPostprocessor
{
private static readonly byte[] Key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
private static readonly byte[] IV = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
private static bool IsEncrypted(string path)
{
using (var fs = new FileStream(path, FileMode.Open))
{
var buffer = new byte[16];
fs.Read(buffer, 0, buffer.Length);
return BitConverter.ToInt64(buffer, 0) == 0x123456789ABCDEF;
}
}
private static void Encrypt(string path)
{
var data = File.ReadAllBytes(path);
using (var aes = new AesManaged())
{
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.PKCS7;
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
{
var encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
using (var fs = new FileStream(path, FileMode.Create))
{
var buffer = BitConverter.GetBytes(0x123456789ABCDEF);
fs.Write(buffer, 0, buffer.Length);
fs.Write(encrypted, 0, encrypted.Length);
}
}
}
}
private static void Decrypt(string path)
{
var data = File.ReadAllBytes(path);
using (var ms = new MemoryStream(data))
{
using (var aes = new AesManaged())
{
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.PKCS7;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
var encrypted = new byte[data.Length - sizeof(long)];
ms.Seek(sizeof(long), SeekOrigin.Begin);
ms.Read(encrypted, 0, encrypted.Length);
var decrypted = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length);
File.WriteAllBytes(path, decrypted);
}
}
}
}
public void OnPreprocessAsset()
{
if (IsEncrypted(assetPath))
{
Decrypt(assetPath);
}
}
public void OnPostprocessAssetbundleNameChanged(string assetPath, string previousAssetBundleName, string newAssetBundleName)
{
if (AssetDatabase.GetImplicitAssetBundleName(assetPath) != null)
{
var path = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) + assetPath;
if (!IsEncrypted(path))
{
Encrypt(path);
}
}
}
}
#endif
```
以上代码将使用AES算法对Asset Bundle进行加密和解密。加密时使用16字节的密钥和向量,并在头部添加一个64位的标识符,以便在解密时检查文件是否已加密。
5. 点击菜单栏中的“File”->“Build Settings”,将要打包的场景添加到场景列表中。
6. 点击“Player Settings”按钮,选择“Publishing Settings”面板,在右侧的Inspector面板中找到“AssetBundle Encryption”部分。
7. 在“AssetBundle Encryption”部分中,将“Encrypt Asset Bundles”选项设置为“True”,并选择“AES128”作为加密算法。
8. 点击“Build”按钮,选择目标平台和输出路径,开始打包Asset Bundle。在打包完成后,所有的Asset Bundle文件都会被加密,并存储在输出路径中。
以上就是使用Unity 2019实现打包Asset Bundle的头部偏移加密的方法。
阅读全文