Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Encryption / Obfuscation of AssetBundles

Discussion in 'Asset Bundles' started by MPTU, Oct 27, 2022.

  1. MPTU

    MPTU

    Joined:
    Oct 5, 2018
    Posts:
    3
    Hi there,

    We are working with large 3D assets currently in the form of AssetBundles which accumulate in the range of 1-5GB in total.

    We have a AUTH2.0 based backend which enables our Unity developed client to download the said AssetBundles as long as the given user has the required privileges to do so.

    The 3D assets are seen as critical data, and therefore we are looking into the options of encrypting or obfuscating the locally cached AssetBundles such that they cannot be accessed outside of the client.

    So far, we have been experimenting with obfuscating the AssetBundles by swapping the bytes around and working with the AssetBundle as a TextAsset (as seen here: https://docs.unity3d.com/540/Documentation/Manual/protectingcontent.html), i.e.:
    Code (CSharp):
    1. public static byte[] Encrypt(byte[] dataToEncrypt)
    2. {
    3.             int mid = (dataToEncrypt.Length + 1) / 2;      
    4.             byte[] firstHalf = dataToEncrypt.Take(mid).ToArray();
    5.             byte[] secondHalf = dataToEncrypt.Skip(mid).ToArray();
    6.             return secondHalf.Concat(firstHalf).ToArray();
    7. }
    8.  
    9. public static byte[] Decrypt(byte[] dataToEncrypt)
    10. {
    11.             int mid = (dataToEncrypt.Length + 1) / 2;
    12.             mid = dataToEncrypt.Length - mid;
    13.             byte[] firstHalf = dataToEncrypt.Take(mid).ToArray();
    14.             byte[] secondHalf = dataToEncrypt.Skip(mid).ToArray();
    15.             return secondHalf.Concat(firstHalf).ToArray();
    16. }
    However, even with a simple approach like this, we experience a ten-fold increase in loading the AssetBundle. Obviously, an increase in loading time is expected, but there must be a better approach.

    As such, I’m curious to hear if anybody else has any good practical experiences with encryption and/or obfuscation of large AssetBundles or Addressables while preserving an acceptable loading speed?
     
  2. MPTU

    MPTU

    Joined:
    Oct 5, 2018
    Posts:
    3
    Bump
     
  3. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    Are you decrypting whole AssetBundle at once? If you need random access you should try CTR mode instead. Also if you're loading large bundles then you will need to use something like mmap() and decrypt the stream on the fly while using AssetBundle API that loads from memory buffer.
     
  4. MPTU

    MPTU

    Joined:
    Oct 5, 2018
    Posts:
    3
    Yes, in this example we are decrypting the whole AssetBundle at once.
    Thanks for providing some pointers in terms of what to look into!
     
  5. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    Just one correction, you should use stream API for loading the asset bundle like LoadFromStream (or it's async version).