2017-08-25 31 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子集時,我剝離了使用加密所需的所有代碼。

相關問題