Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aristojoyce · Jun 22, 2015 at 10:51 AM · iosnullreferenceexceptionil2cppencryptioncryptography

AES encryption in iOS - NullReferenceException

I'm recently trying to upgrade my iOS project to support 64-bit, my unity version is v4.6.6p2. When we export an Xcode project using il2cpp and run the App on the iPad, it always catch a NullReferenceException on the AES.create() line.

I have tried 32-bit and 64-bit iPad, they are all the same. But the code run perfectly on the editor and mono project.

here is my code:

     using UnityEngine;
     using System;
     using System.Collections;
     using System.Collections.Generic;
     using System.Text.RegularExpressions;
     using System.Security.Cryptography;
     using System.Text;
     using System.IO;
     
     public class testEncrypt : MonoBehaviour {
         public UILabel label;
         private string _Key = "somekeystring";
     
     
         // Use this for initialization
         void Start () {
             try {
             label.text = Encrypt("testing");
             }
             catch (Exception ex) {
                 Debug.Log("error: " + ex.ToString());
             }
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     
     
             
         private string createKey(string key) {
             MD5 _md5 = null;
             try {
                 _md5 = MD5.Create();
             }
             catch (Exception ex) {
                 Debug.Log("md5 error: " + ex.ToString());
             }
             byte[] source = Encoding.Default.GetBytes(key);
             byte[] crypto = null;
             try {
                 crypto = _md5.ComputeHash(source);
             }
             catch (Exception ex) {
                 Debug.Log("computeHash: " + ex);
             }
             string result = Convert.ToBase64String(crypto);
             return result;
         }
     
         private static byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p) {
             byte[] kRaw = suggestedKey;
             System.Collections.Generic.List<byte> kList = new System.Collections.Generic.List<byte>();
             
             for (int i = 0; i < p.LegalKeySizes[0].MinSize; i += 8) {
                 kList.Add(kRaw[(i / 8) % kRaw.Length]);
             }
             byte[] k = kList.ToArray();
             return k;
         }
         
         private string Encrypt(string clearText) {
             string EncryptionKey = _Key;
             byte[] clearBytes = Encoding.ASCII.GetBytes(clearText);
                 
             try {
             //    Aes encryptor = Aes.Create();
             using (Aes encryptor = Aes.Create()) {
                 //Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {/* hide */});
                 encryptor.BlockSize = /* hide */;
                 encryptor.KeySize = /* hide */;
                 encryptor.Mode = CipherMode.CBC;
                 encryptor.Padding = PaddingMode.PKCS7;
                 encryptor.IV = new byte[] {/* hide */};
                 encryptor.Key = GetKey(Encoding.Default.GetBytes(_Key), encryptor);
                     
                 using (MemoryStream ms = new MemoryStream()) {
                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) {
                         cs.Write(clearBytes, 0, clearBytes.Length);
                         cs.Close();
                     }
                     clearText = Convert.ToBase64String(ms.ToArray());
                 }
             }
             }
             catch (Exception ex) {
                 Debug.Log("aes error: " + ex);
             }
             return clearText;
         }    
     }
     

Exception catch: aes error: System.NullReferenceException: A null value was found where an object instance was required.

I have googled for sometime and to my surprise I can only find very very few question/report on this issue. I'm now stuck and don't know what to do. Is it a bug that I need to file or is it something I did wrong to run into this problem? Is there any work around or sample that I can use?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by JoshPeterson · Jun 23, 2015 at 10:29 AM

This problem occurs because the AES code is accessed via reflection, and is stripped out of the System.Core.dll assembly in this case (as stripping is always enabled with the IL2CPP scripting backend). In order to prevent this code from being stripped, add a link.xml file to the project (in the Assets folder) with the following contents:

 <linker>
   <assembly fullname="System.Core">
     <type fullname="System.Security.Cryptography.AesManaged" preserve="all"/>
   </assembly>
 </linker>

More general documentation about the link.xml file is available here: http://docs.unity3d.com/Manual/iphone-playerSizeOptimization.html.

We will correct this issue though, so that this link.xml file will not be necessary in a future release.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aristojoyce · Jun 25, 2015 at 03:06 AM 0
Share

This work!!! Thank you very much!!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NullReferenceException il2cpp 0 Answers

64 bit build iOS in Unity free 1 Answer

Can I enable null reference checking in production code on iOS 1 Answer

IL2CPP: PROPERTYINFO.SETVALUE SETS A RANDOM NUMBER TO A NULLABLE VALUE TYPE WHEN NULL IS EXPECTED. 0 Answers

Crash on Prime31 Twitter login when built with il2cpp 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges