Search Unity

Rfc2898DeriveBytes.GetBytes raised an exception under Android

Discussion in 'Android' started by NicolasHognon, Apr 6, 2016.

  1. NicolasHognon

    NicolasHognon

    Joined:
    Nov 15, 2010
    Posts:
    32
    Hello,

    I need to encrypt some data for my game (before storing it).
    Everything is ok in the editor.
    But once it run on my test device it raised an exception.

    It seems that this code is not ok

    var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations);
    var keyBytes = password.GetBytes(Keysize / 8);

    Here is my logs

    Exception catched GetBytes: Object reference not set to an instance of an object
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.HMAC.Initialize () [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.HMAC.HashCore (System.Byte[] rgb, Int32 ib, Int32 cb) [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.HashAlgorithm.ComputeHash (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.HashAlgorithm.ComputeHash (System.Byte[] buffer) [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.Rfc2898DeriveBytes.F (System.Byte[] s, Int32 c, Int32 i) [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at System.Security.Cryptography.Rfc2898DeriveBytes.GetBytes (Int32 cb) [0x00000] in <filename unknown>:0
    04-06 23:48:54.560 28450 28495 E Unity : at StringCipher.Encrypt (System.String plainText, System.String passPhrase) [0x000b9] in /Users/nicolas/dev/git/project/Assets/Scripts/Managers/PlayerGameData.cs:326

    If found something quite similar here
    http://answers.unity3d.com/questions/991254/aes-encryption-in-ios-nullreferenceexception.html
    but it seems to be related to iOS only ... which is not my case (yet).

    @JoshPeterson, as you answered the question about AES under iOS perhaps you have an idea ?

    Do someone else as any idea ?

    I am using Unity3D 5.2.4f1
    And my test device is a nexus 5 with android 6.0.1

    Thanks
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    @NicolasHognon

    I'm afraid that I can't offer too much help here. The other case was not specific to iOS, but actually related to the use of managed code stripping. You can try adding a link.xml file as I suggested in that case, but if that does not work, then I'm not sure what the cause of the problem is.

    I guess a more general question is in order though. What is the "Stripping Level" set to in the Player Settings? If it is disabled, then this is not a stripping issue.
     
  3. NicolasHognon

    NicolasHognon

    Joined:
    Nov 15, 2010
    Posts:
    32
    ok the problem was due to the stripping of the player
    if a disable it everything is ok
    thanks
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    @NicolasHognon

    I'm glad you were able to solve the issue!
     
  5. NicolasHognon

    NicolasHognon

    Joined:
    Nov 15, 2010
    Posts:
    32
    In fact I have not solved this problem.
    I try to use link.xml but I continue to have the exception raised.
    Cannot find what to add to link.xml to solve the problem.
    Or the problem is not directly linked to link.xml file but perhaps to the code it self.

    If I look for this king on problem usign google, it seems I am not the only one to encouter problems when using encryption with stripped player. But I am the only one to use "Rfc2898DeriveBytes". Perhaps I will think to change to encryption code.
     
  6. NicolasHognon

    NicolasHognon

    Joined:
    Nov 15, 2010
    Posts:
    32
    in fact I found the solution ... a little problem in the link.xml file ...
     
  7. geetikak

    geetikak

    Joined:
    Jun 16, 2016
    Posts:
    2
    I am also facing the same issue. Can you please share the fix?
     
  8. geetikak

    geetikak

    Joined:
    Jun 16, 2016
    Posts:
    2
    Kindly share what the issue is
     
  9. NicolasHognon

    NicolasHognon

    Joined:
    Nov 15, 2010
    Posts:
    32
    here is the link.xml is use
    Code (CSharp):
    1. <linker>
    2.     <assembly fullname="mscorlib">
    3.         <namespace fullname="System.Security.Cryptography" preserve="all"/>
    4.     </assembly>
    5. </linker>