Dot-Net

在 .NET 核心中實現 RSA

  • February 1, 2017

我正在嘗試使用 RSA 加密和解密一些數據。我查了一下RSA類,但我只看到抽像類<https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa(v=vs.110).aspx>

我讀過 DNX5 中的 RSA 類與 .net 4.6.1 中的不同,這與我看到的不同嗎?如果是這樣,我在哪裡可以找到使用該文件的文件?它似乎也RSACryptoServiceProvider不能在 .net 核心中工作,我只能訪問RSA抽像類。

RSACryptoServiceProvider如果可以的話,你應該避免使用。它僅適用於 Windows(它是 Windows 上不太好的 RSA 實現)。堅持RSA基類,並通過創建新實例RSA.Create()

臨時密鑰(創建)

.NET 核心

using (RSA rsa = RSA.Create())
{
   rsa.KeySize = desiredKeySizeInBits;

   // when the key next gets used it will be created at that keysize.
   DoStuffWithThePrivateKey(rsa);
}

.NET 框架

不幸的是,.NET Framework 上 RSA.Create() 的預設值是 RSACryptoServiceProvider,它不遵守 set_KeySize。因此,如果您需要臨時密鑰,則需要在 .NET Framework 和 .NET Core 上使用不同的程式碼:

using (RSA rsa = new RSACng())
{
   rsa.KeySize = desiredKeySizeInBits;

   DoStuffWithThePrivateKey(rsa);
}

或者,如果您需要支持早於 4.6(RSACng 不存在)/4.6.2(大多數 .NET Framework 使用 RSACng 對象而不是 RSACryptoServiceProvider 對象)的版本,您可以繼續使用舊的實現:

using (RSA rsa = new RSACryptoServiceProvider(desiredKeySizeInBits))
{
   // Since before net46 the Encrypt/Decrypt, SignData/VerifyData, SignHash/VerifyHash
   // methods were not defined at the RSA base class, you might need to keep this strongly
   // typed as RSACryptoServiceProvider.
   DoStuffWithThePrivateKey(rsa);
}

臨時密鑰(導入)

儘管 RSACng 通常比 RSACryptoServiceProvider 更容易使用,但 RSACryptoServiceProvider 在這種情況下應該可以正常工作,因此 RSA.Create() 在所有平台上都很好:

using (RSA rsa = RSA.Create())
{
   rsa.ImportParameters(rsaParameters);

   DoStuffWithWhateverKindOfKeyYouImported(rsa);
}

從證書:

.NET Core 1.0+、.NET Framework 4.6+

using (RSA rsa = cert.GetRSAPublicKey())
{
   DoStuffWithThePublicKey(rsa);
}

要麼

using (RSA rsa = cert.GetRSAPrivateKey())
{
   DoStuffWithThePrivateKey(rsa);
}

.NET 框架 < 4.6

// Do NOT put this in a using block, since the object is shared by all callers:
RSA rsaPrivate = (RSA)cert.PrivateKey;
DoStuffWithThePrivateKey(rsaPrivate);

// Do NOT put this in a using block, since the object is shared by all callers:
RSA rsaPublicOnly = (RSA)cert.PublicKey.Key;
DoStuffWithThePublicKey(rsaPublic);

使用命名/持久密鑰(僅限 Windows)

我將包含一些關於 RSACryptoServiceProvider (WinXP+/CAPI) 和 RSACng (Win7+/CNG) 創建/打開命名密鑰的範例,但這在 .NET 中不是很常見的場景;而且它當然不是可移植的(對其他作業系統的可移植性是 .NET Core 更引人注目的論點之一)。

參考事物。

對於 .NET Core 1.0 和 1.1,您可以從包中訪問 RSA 基類System.Security.Cryptography.Algorithms。在 .NET Core 2.0 中,它將包含在netstandard包參考中。

如果您需要與作業系統進行複雜的互操作,您可以參考System.Security.Cryptography.Cng(Windows CNG)、System.Security.Cryptography.Csp(Windows CAPI/CryptoServiceProvider)或System.Security.Cryptography.OpenSsl(Linux OpenSSL、macOS OpenSSL)並訪問啟用互操作的類(RSACng、RSACryptoServiceProvider、RSAOpenSsl)。但是,真的,你不應該那樣做。

RSA.Create() 返回什麼?

  • .NET Framework:RSACryptoServiceProvider,除非由 CryptoConfig 更改。
  • .NET Core (Windows):通過 CNG 實現 RSA 的私有類,您不能將其轉換為任何更具體的類型。
  • .NET Core (Linux):通過 OpenSSL 實現 RSA 的私有類,您不能將其轉換為任何更具體的類型。
  • .NET Core (macOS):通過 OpenSSL 實現 RSA 的私有類,您不能將其轉換為任何更具體的類型。(這應該在 .NET Core 的下一版本中通過 SecureTransforms)

引用自:https://stackoverflow.com/questions/41986995