- Home /
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?
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.
Your answer
Follow this Question
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