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 kList = new System.Collections.Generic.List();
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?
↧