Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IL2CPP RSACryptoServiceProvider

Discussion in 'iOS and tvOS' started by PixelSquad, Mar 6, 2015.

  1. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Hi guys

    Did anyone manage to use RSACryptoServiceProvider with IL2CPP?

    The following code results in an exception

    public static void SimpleTest()
    {
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

    string message = "SimplerTest";
    byte[] unencrypted = Encoding.UTF8.GetBytes(message);
    byte[] encrypted = rsa.Encrypt(unencrypted, false);
    byte[] decryptedPacket = rsa.Decrypt(encrypted, false);
    message = Encoding.UTF8.GetString(decryptedPacket);

    DEDebug.Log("Decrypted message = " + message);
    }

    CryptographicException: PKCS1 decoding error.
    at System.Security.Cryptography.HashAlgorithm.ComputeHash (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
    at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.SetKey (System.Security.Cryptography.AsymmetricAlgorithm key) [0x00000] in <filename unknown>:0
    at System.Globalization.GregorianCalendar.GetYear (DateTime time) [0x00000] in <filename unknown>:0
    at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt (System.Byte[] rgb, Boolean fOAEP) [0x00000] in <filename unknown>:0
    at RSATest.SimplerTest () [0x00000] in <filename unknown>:0
    at RSATest.Start () [0x00000] in <filename unknown>:0


    I'd appreciate if anyone could share a workaround if this is a bug that still hasn't been fixed. Thanks!
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    Which version of Unity are you using? We did have a bug which caused this problem in 4.6.3 and 5.0. The bug was corrected in the 4.6.3p1 release (although I would recommend using 4.6.3p2, since it is out now). We're planning to ship a fix for 5.0 in the next 5.0 patch release as well.
     
  3. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Thanks Josh.

    We're using unity 5. When is the expected release date for the next 5.0 patch?
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    This should be out by the end of next week. As always, we could slip the schedule, but we don't plan to.
     
  5. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Hi again Josh, do you also know of a bug with IL2CPP and System.Security.Cryptography.MD5?

    Perhaps this is related as I found that the following code is also not working:

    using (System.Security.Cryptography.MD5md5 = System.Security.Cryptography.MD5.Create())
    {
    hash = BitConverter.ToString(md5.ComputeHash(d)).Replace("-", String.Empty);
    }
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    Yes, this bug should also be fixed in the next patch release.
     
  7. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    177
    Hi, I've issue with il2cpp in a process using RSACryptoServiceProvider (everything is ok with mono), We are using 4.6.4p4. The status of the bug is still Status: Fixed in future release.
    We have not this specific exception, but a nullref after encryption (i'm tracking for more informations).

    Did you know if there is still known issues with crypto AES and/or RSA ?

    Edit : I found my issue AES.Create() return null;

    Bug Id 693259
     
    Last edited: Apr 29, 2015
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    @paradizIsCool

    Thanks for submitting this bug. We were not aware of any encryption-related bugs, so we will investigate this.
     
  9. aristojoyce

    aristojoyce

    Joined:
    Nov 22, 2013
    Posts:
    1
    I don't know where to put this so I just put as a reply here.

    I'm now using the v4.6.6p2, and I'm STILL encountering this issue.

    When I tried to generate iOS project using il2cpp backend, the AES.create() function always return a NullReferenceException.

    I'm also surprised that when I googled this problem, I only found very few related posts and for those I found they barely have any solution!!! And they are relatively OLD post (v4.6.2). Isn't that AES encrytion a common practice? Even on the Unity forum, there is very few reports, no fix, no solution.

    I don't know if this problem exists in 5.x, but it is not an option for me.

    I'm really frustrated now since Apple required the 64-bit support and I can't overcome the encryption problem which I think is pretty important!

    So I really wondered if it is due to a bug (which is not solved for MONTHS) or is it a miss use of the function by me (however the code runs perfectly in Mono). If it is a miss use I really want some sort of sample code or work around.
     
  10. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    The solution I could use at the time was to roll back to Unity 4 and use mono as Apple was still accepting 32-bit binaries until the end of May.

    The severity of this problem has now grown to *extreme* as we will not be able to ship our next update which is due soon without this being fixed.
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    @aristojoyce

    Thanks for bringing this up again. I suspect that this is a bug, since it works with Mono. I also suspect that the same problem happens in the 5.x releases, as the IL2CPP code is the same across 4.6 and 5.x.

    I apologize that this bug has been around for so long without any response. I've poked our QA team now to investigate it, and we should be able to look at getting a fix soon.
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    We have now investigated this issue (sorry that it went so long). 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.
     
  13. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Thanks Josh
     
  14. kingzyt

    kingzyt

    Joined:
    Aug 26, 2015
    Posts:
    1
    hello
    i add 'AesManaged' into link.xml, but still get "CryptographicException: PKCS1 decoding error"
    does it not work in 4.6.3? or i miss something?
    thank you
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,747
    @kingzyt

    This should work, although 4.6.3 is a bit old by now. There might be a fix in a later version that you need as well, I'm not certain. Can you provide the full content of your link.xml file here? Then we can verify that it is correct.