2017-08-25 62 views
-1

在Unity android build中使用AES解密字符串时获取空引用异常。它的一些HMAC初始化错误。Unity C#:HMAC初始化抛出并出错。如何解决它

我已经使用system.cryptorgraphy的AES加密和解密算法在我的游戏中使用加密,并且在Android设备上出现错误。有没有人有关于什么是HMAC Initialise()的想法以及如何解决这个错误?我粘贴了我用来解密的代码。

错误的屏幕截图全部附在下面。

public string Decrypt (string cipherText) 
{ 
    string EncryptionKey = "abc123"; 
    cipherText = cipherText.Replace (" ", "+"); 
    byte[] cipherBytes = Convert.FromBase64String (cipherText); 
    using (Aes encryptor = Aes.Create()) { 
     Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes (EncryptionKey, new byte[] { 
      0x49, 
      0x76, 
      0x61, 
      0x6e, 
      0x20, 
      0x4d, 
      0x65, 
      0x64, 
      0x76, 
      0x65, 
      0x64, 
      0x65, 
      0x76 
     }); 
     encryptor.Key = pdb.GetBytes (32); 
     encryptor.IV = pdb.GetBytes (16); 
     using (MemoryStream ms = new MemoryStream()) { 
      using (CryptoStream cs = new CryptoStream (ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { 
       cs.Write (cipherBytes, 0, cipherBytes.Length); 
       cs.Close(); 
      } 
      cipherText = Encoding.Unicode.GetString (ms.ToArray()); 
     } 
    } 
    return cipherText; 
} 

enter image description here

我很新的#1,我试图寻找重复,但我失败了。如果您认为问题重复,请提供链接。

+0

我建议你阅读[如何问完美问题](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)和[如何创建最小,完整和可验证例如](https://stackoverflow.com/help/mcve),然后快速编辑您的问题,然后再进行downvoted和burried。 –

+0

感谢您的建议,请通过它。 –

+0

在* Rfc2898DeriveBytes中使用* HMAC来从给定密码(和盐)生成密钥。这并不能解释为什么它没有被初始化。并且它也不解释为什么首先调用哈希,然后解释HMAC。我可以给你的唯一想法是还有一个构造函数,它另外需要一个迭代计数(一个整数落在你的静态盐值后面,比如设置为40,000左右)。请注意,静态盐几乎无用。 –

回答

0

我得到了解决方案。我得到空ref,因为使用HMAC .Net2.0使用反射,当我正在构建我使用.Net 2.0子集时,我剥离了使用加密所需的所有代码。

相关问题