unity3d与flash交互、aes加密解密
发布时间:2020-12-15 17:28:39 所属栏目:百科 来源:网络整理
导读:众所周知,unity在2013年就放弃flash了,但是有时候,真的会需要到flash,比如web端需要unity web player插件,很多浏览器都拦截了,flash倒不会,因为他是微软的。 u3d在打包flash的时候api是不可以修改的,默认就是flash的api,打包出来就是as(ActionScrip
|
众所周知,unity在2013年就放弃flash了,但是有时候,真的会需要到flash,比如web端需要unity web player插件,很多浏览器都拦截了,flash倒不会,因为他是微软的。 u3d在打包flash的时候api是不可以修改的,默认就是flash的api,打包出来就是as(ActionScript代码)编译。所以很多net2.0的api都不能使用,比如System.Security.Cryptography这个库,注:这个库是C#集成好的加密解密 u3d导出flash会报错,因为不支持 ? ? 资源加密在任何平台任何程序下都是常见的,我这里使用的AES加密。大部分平台都集成了加密解密库, flash的as加密解密库,下载地址:as crypt?其中很多种方式加密解密。比如 aes、des等等,我使用的aes加密解密 在assets下新建ActionScript文件夹,把下载as加密解密库的放进去, 测试用例, c#代码
using UnityEngine;
using System.Collections;
[NotConverted]
[NotRenamed]
public static class AesCtypt {
[NotRenamed]
public static string response = "5";
[NotRenamed]
public static byte[] AESDecrypt(byte[] cipherText,string strKey,byte[] _key1)
{
return null;
}这个是u3d的脚本语言,解密用,
[NotConverted] package
{
import flash.utils.ByteArray;
import System.CLIArrayFactory;
import System.CLIByteArray;
import com.hurlant.util.Hex;
import com.hurlant.crypto.symmetric.IPad;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.IVMode;
import com.hurlant.crypto.symmetric.NullPad;
import com.hurlant.crypto.Crypto;
public class AesCtypt
{
public static var response : String = "";
public function AesCtypt()
{
}
public static function AESDecrypt(DesByteData:CLIByteArray,SECRET_KEY:String,SECRET_IV:CLIByteArray):CLIByteArray
{
var inputBA:ByteArray= DesByteData.elements;
var key:ByteArray = Hex.toArray(Hex.fromString(SECRET_KEY));
var pad:IPad = new NullPad();
var aes:ICipher = Crypto.getCipher("aes-cbc",key,pad);
var ivmode:IVMode = aes as IVMode;
ivmode.IV = SECRET_IV.elements;
aes.decrypt(inputBA);
var aesbytedata:CLIByteArray = new CLIByteArray();
aesbytedata.elements= inputBA;
return aesbytedata;
}
}
}
这是flash端的as脚本,跟C#端的脚本方法变量完全相同,因为打包完成后,会调用as脚本,不会再调用C#的东西了,在unity实现脚本,就是为了不报错,和本地测试使用,打出包后,他调用的就是as代码了。加密跟解密一样直接调用aes.encrypt方法就可以了 测试用例 using UnityEngine;
using System.Collections;
public class LoadFlashAssets : MonoBehaviour {
string str = "";
public void OnGUI()
{
if(GUI.Button(new Rect(220,10,100,25),"Calculate"))
{
string path = "http://100.0.0.0:10001/assets/test.u3d”;//资源路径地址
StartCoroutine(LoadFlashAsset(path));
}
GUI.Label(new Rect(220,200,200),str);
}
private string key_3D = “你的密码”;
IEnumerator LoadFlashAsset(string path)
{
WWW loader = new WWW(path);
yield return loader;
if (loader.error == null && loader.isDone)
{
AssetBundleCreateRequest request = null;
AssetBundle bundle = null;
byte[] _key1 = { 这里是你的iv密匙 };
byte[] decryptedDatas = AesCtypt.AESDecrypt(loader.bytes,key_3D,_key1);
int length = System.Convert.ToInt32(decryptedDatas[decryptedDatas.Length - 1]);
byte[] decryptedData = new byte[decryptedDatas.Length - 1 - length];
for(int i = 0; i < decryptedData.Length; i ++)
{
decryptedData[i] = decryptedDatas[i];
}
request = AssetBundle.CreateFromMemory(decryptedData);
yield return request;
bundle = request.assetBundle;
if (bundle != null)
{
GameObject obj = GameObject.Instantiate(bundle.mainAsset) as GameObject;
}
}
}
} 这样u3d导出flash的时候,就能实现u3d在flash环境下的加密解密,研究了大概三天才搞定,语言表达能力有限,写的看不明白的朋友大家留言交流
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
