【问题标题】:Xamrin UWP Release causes weird NullReferenceException (System.Security.Cryptography.PasswordDeriveBytes)Xamarin UWP Release 导致奇怪的 NullReferenceException (System.Security.Cryptography.PasswordDeriveBytes)
【发布时间】:2023-03-16 18:20:02
【问题描述】:
public static class CryptoHelper {
    // This size of the IV (in bytes) must = (keysize / 8).  Default keysize is 256, so the IV must be
    // 32 bytes long.  Using a 16 character string here gives us 32 bytes when converted to a byte array.
    private const string initVector = "pemgail9uzpgzl88";
    // This constant is used to determine the keysize of the encryption algorithm
    private static int keysize = 256;

    private static int getKeySize()
    {
        return 256;
    }

    //Encrypt
    //public static byte[] EncryptString( string plainText, string passPhrase ) {
    public static byte[] EncryptString(string toEncrypt, string salt)
    {
        byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(toEncrypt);

        byte[] keyBytes = new byte[126];
        try
        {
            PasswordDeriveBytes password = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(salt), null);

            Debug.WriteLine(CryptoHelper.getKeySize());
            Debug.WriteLine(password.ToString());
            keyBytes = password.GetBytes(256 / 8);
        } catch (Exception e)
        {
            Debug.WriteLine(e.StackTrace);
        }
        

        
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        return cipherTextBytes;
    }
 ........

对“password.GetBytes(256 / 8);”的调用导致不可捕获的 NullReferenceException 仅当 UWP 应用处于发布模式时才会发生这种情况; UWP Debug 以及 Andorid 和 IOS 都可以。

我还收到一条奇怪的调试消息:

“this._hash”战争“null”。

“this._hash”为“null”。 (翻译)

在这里您可以看到它的实际效果 VS2019 Screenshot

为了避免这个问题,函数的输入是:

toEncrypt 强> “承载eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiZXhwIjoxNjE3MDAyMTEyfQ.C0CaGgfibM4z55MoANI2CiohUyew09r3_D_TpcQ6n1c8LmQd8BusSyF1SMEIQ3cO5uxE9Tnau0ZAT6D3kN3NcQ” P>

"9x83m74tzrx9387x4mz98374zt90x8m273z948734z59"

因为我看不到这个问题的详细原因,所以无法找到解决方法。

【问题讨论】:

    标签: debugging release nullreferenceexception xamarin.uwp


    【解决方案1】:

    我试图让相同的代码工作。我找到的解决方案是替换:

    PasswordDeriveBytes password = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(salt), null);
    

    与:

     Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, Encoding.UTF8.GetBytes("12345678"));
    

    并添加以下内容:

    symmetricKey.Padding = PaddingMode.Zeros;
    

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 2014-10-09
      • 2013-11-24
      • 2017-02-10
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多