Unity3d and Flash interaction, AES encryption and decryption

Source: Internet
Author: User

As we all know, unity gave up flash in 2013, but sometimes it really needs to flash, such as the web-side needs Unity Web Player plugin, many browsers are blocked, Flash will not, because he is Microsoft.

U3D when packaging Flash API is not modifiable, the default is the Flash API, packaged out is as (ActionScript code) compiled. So many net2.0 API can not be used, such as System.Security.Cryptography This library, note: This library is C # integrated good encryption and decryption

U3D export Flash will error, because does not support

Resource encryption is common in any platform under any program, and I use AES encryption here. Most platforms have an integrated encryption and decryption library,

Flash as encryption and decryption Library : ascrypt Many ways of encrypting and decrypting. such as AES, Des and so on, I use the AES encryption decryption

Create a new ActionScript folder under assets, and put the download as encryption and decryption library

Test cases,

C # code

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;}
This is U3d's scripting language, decryption,

[notconverted]
[notrenamed]

This flag indicates that the current class is packaged and compiled, not compiled into a packaged environment script, preserving the original script, but this will not be executed, the bottom says why not execute

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 ;p ublic class Aesctypt{public static var response:string = "";p ublic function Aesctypt () {}public static function aesdecr Ypt (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; } }}

This is the flash end of the as script, and the C # side of the script method variables exactly the same, because after the packaging is done, will call the as script, no longer call the C # thing, in unity implementation of the script is to not error, and local test use, after the package, he called the as code. Encryption is just as straightforward as decryption by calling the Aes.encrypt method.

Test Cases

Using unityengine;using System.collections;public class Loadflashassets:monobehaviour {string str = "";p ublic void OnGUI () {if (GUI. button (new Rect (220,10,100,25), "Calculate")) {string path = "http://100.0.0.0:10001/assets/test.u3d";// Resource path Address Startcoroutine (Loadflashasset (path));} Gui. Label (New Rect (220,100,200,200), str);} private string key_3d = "Your password"; 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 = {Here is your IV key};byte[] Decrypteddatas = Aesctypt.aesdecrypt (Loader.bytes, Key_3D,_k ey1); 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;}}}} 

In this way u3d export flash, you can achieve u3d in the flash environment of encryption and decryption, study about three days before the deal, language capacity is limited, written to see not understand the friends of the message exchange

Unity3d and Flash interaction, AES encryption and decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.