Search Unity

Encryption/Decryption

Discussion in 'Asset Bundles' started by Tyndareus, Sep 11, 2019.

  1. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    12
    My current project is a deployed content project rather than a downloaded project, and the locations may or may not have internet so a license key is provided and they is the basis for decryption.

    I have two options available at the moment.
    Code (CSharp):
    1. SymmetricAlgorithm alg = TripleDES.Create();
    2. alg.Mode = CipherMode.ECB;
    3. ICryptoTransform trans = alg.CreateEncryptor(key, vector); //Then in decrypt replace CreateEncryptor with CreateDecryptor
    4. return trans.TransformFinalBlock(bytes, 0, bytes.Length);
    First one works, granted it takes a lifetime to decrypt large bundles.

    Second option is much faster but Unity seems to not like it
    Code (CSharp):
    1. using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    2. {
    3.     aesAlg.Key = Key;
    4.     aesAlg.IV = IV;
    5.     ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
    6.  
    7.     using (MemoryStream msDecrypt = new MemoryStream())
    8.     {
    9.         using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
    10.         {
    11.             csDecrypt.Write(encrypted, 0, encrypted.Length);
    12.             csDecrypt.FlushFinalBlock();
    13.         }
    14.  
    15.         decryptedBytes = msDecrypt.ToArray();
    16.     }
    17. }
    And replace any of the above from decrypt to encrypt

    Encryption and Decryption seem fine, using the AesCryptoServiceProvider seems to have a huge speed increase over the TransformFinalBlock method.
    However after decrypting i pass the bytes over to AssetBundle.LoadFromMemoryAsync(decryptedBytes), the bundle manager just complains saying "Failed to decompress data for the asset bundle 'memory'".
    Mixed results when searching google for this but all I can assume is that during the Aes encryption/decryption theres some byte compression that causes the bundle to lose something that it needs for unity to be able to read it back.

    Any help with this or people that have ran into this problem before would be good.