mys*_*ick 27 .net encryption cryptography rsa asp.net-core
我正在尝试使用RSA加密和解密一些数据.我看了RSA
上课,但我只看到抽象类https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa(v=vs.110).aspx
我已经读过DNX5中的RSA类与.net中的RSA类不同4.6.1这与我所看到的不同吗?如果是这样,我在哪里可以找到使用该文档的文档?它似乎RSACryptoServiceProvider
也不适用于.net核心,我只能访问RSA
抽象类.
bar*_*njs 65
RSACryptoServiceProvider
如果可以,你应该避免使用.它只适用于Windows(这是Windows上不太好的RSA实现).坚持RSA
基类,并通过创建新实例RSA.Create()
using (RSA rsa = RSA.Create())
{
rsa.KeySize = desiredKeySizeInBits;
// when the key next gets used it will be created at that keysize.
DoStuffWithThePrivateKey(rsa);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,.NET Framework上的RSA.Create()的默认值是RSACryptoServiceProvider,它不尊重set_KeySize.因此,如果您需要临时密钥,则需要在.NET Framework和.NET Core上使用不同的代码:
using (RSA rsa = new RSACng())
{
rsa.KeySize = desiredKeySizeInBits;
DoStuffWithThePrivateKey(rsa);
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您需要支持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);
}
Run Code Online (Sandbox Code Playgroud)
尽管RSACng通常比RSACryptoServiceProvider更容易使用,但RSACryptoServiceProvider在此上下文中应该可以正常工作,因此RSA.Create()在所有平台上都很好:
using (RSA rsa = RSA.Create())
{
rsa.ImportParameters(rsaParameters);
DoStuffWithWhateverKindOfKeyYouImported(rsa);
}
Run Code Online (Sandbox Code Playgroud)
using (RSA rsa = cert.GetRSAPublicKey())
{
DoStuffWithThePublicKey(rsa);
}
Run Code Online (Sandbox Code Playgroud)
要么
using (RSA rsa = cert.GetRSAPrivateKey())
{
DoStuffWithThePrivateKey(rsa);
}
Run Code Online (Sandbox Code Playgroud)
// 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);
Run Code Online (Sandbox Code Playgroud)
我打算包含一些关于RSACryptoServiceProvider(WinXP +/CAPI)和RSACng(Win7 +/CNG)创建/打开命名密钥的示例,但这不是.NET中非常常见的情况.它当然不可移植(其他操作系统的可移植性是.NET Core更引人注目的参数之一).
对于.NET Core 1.0和1.1,您可以从System.Security.Cryptography.Algorithms
包中访问RSA基类.在.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).但是,真的,你不应该这样做.
您将需要改用 dotnetcore 库。将System.Security.Cryptography.Algorithms
Nuget 包添加到您的项目中。你可以在这里找到它:System.Security.Cryptography.Algorithms
它为您的项目提供了所有通用算法。
这是您将用于 Encrypt() 和 Decrypt() 方法的RSACryptoServiceProvider 类。
归档时间: |
|
查看次数: |
21362 次 |
最近记录: |