我正在嘗試將我的 .net android 應用程式從“android xamarin 應用程式”遷移到 .net 6.0。我終于進行了構建,現在每次從位元組陣列讀取 X509 證書時它都會下降:
return new X509Certificate(bytes);
我確信證書是有效的,因為在遷移之前沒有問題。例外的堆疊跟蹤是:
System.PlatformNotSupportedException: Cryptography_AlgorithmNotSupported, RC2
at System.Security.Cryptography.PasswordBasedEncryption.CreateRC2()
at System.Security.Cryptography.PasswordBasedEncryption.Decrypt(AlgorithmIdentifierAsn& algorithmIdentifier, ReadOnlySpan`1 password, ReadOnlySpan`1 passwordBytes, ReadOnlySpan`1 encryptedData, Span`1 destination)
Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptAndProcessSafeContents(ReadOnlySpan`1 password, CertBagAsn[]& certBags, AttributeAsn[][]& certBagAttrs, Int32& certBagIdx, SafeBagAsn[]& keyBags, Int32& keyBagIdx)
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
at Internal.Cryptography.Pal.UnixPkcs12Reader.VerifyAndDecrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
Exception_EndOfInnerExceptionStack
at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
at Internal.Cryptography.Pal.AndroidCertificatePal.ReadPkcs12(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean ephemeralSpecified)
at Internal.Cryptography.Pal.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at Internal.Cryptography.Pal.CertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] data)
...
我試圖找到有關它的一些資訊,只是發現我無法在 runtime/src/libraries/Common/src/Internal/Cryptography/Helpers.cs 檔案中創建“RC2”:
#if NET5_0_OR_GREATER
[UnsupportedOSPlatformGuard("android")]
[UnsupportedOSPlatformGuard("browser")]
public static bool IsRC2Supported => !OperatingSystem.IsAndroid();
#else
public static bool IsRC2Supported => true;
#endif
所以問題是:
有誰知道為什么這個代碼不受支持?
我可以在我的 .net 6.0 android 應用程式中以某種方式使用 x509 證書嗎?
uj5u.com熱心網友回復:
有誰知道為什么這個代碼不受支持?
適用于 Android 的 .NET 6 使用 Android 內置加密提供程式,這些提供程式不包括 RC2。
現在每次我從位元組陣列讀取 X509 證書時它都會下降
從技術上講,您的位元組不是證書。它們是 PFX,并且該 PFX 中的證書是用 40 位 RC2 加密的(一種非常弱的演算法,它也可能是 ROT13,但這是 1990 年代美國密碼法規的平衡行為......然后傳統)。
我可以在我的 .net 6.0 android 應用程式中以某種方式使用 x509 證書嗎?
如果您在 Windows、Linux 或 macOS 的 .NET 6 中將 PFX 加載為可匯出的,然后將其重新匯出為新的 PFX,它應該可以在 Android 上運行;因為所有現代堆疊都應該使用 3DES 而不是 RC2-40(他們仍然使用 3DES 而不是 AES,因為并非所有市場上的作業系統版本都將 AES 連接到他們的 PFX 讀取器)。
X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Import(bytes, pwd, X509KeyStorageFlags.Exportable);
byte[] newPfx = coll.Export(X509ContentType.Pfx, pwd);
或者,如果您使用 OpenSSL 的命令列實用程式來構建您的 PFX,請添加-certpbe 3DES到您的命令列(例如openssl pkcs12 -export -in cert.cer -inkey cert.key -certpbe 3DES -out cert.pfx)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365816.html
