【问题标题】:implement RSA in .NET core在 .NET 核心中实现 RSA
【发布时间】:2017-06-18 15:01:22
【问题描述】:

我正在尝试使用 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 抽象类。

【问题讨论】:

    标签: .net encryption asp.net-core cryptography rsa


    【解决方案1】:

    如果可以的话,你应该避免使用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 框架
    // 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,您可以从 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)。但是,真的,你不应该那样做。

    RSA.Create() 返回什么?

    • .NET Framework:RSACryptoServiceProvider,除非由 CryptoConfig 更改。
    • .NET Core (Windows):通过 CNG 实现 RSA 的私有类,您不能将其转换为任何更具体的类型。
    • .NET Core (Linux):通过 OpenSSL 实现 RSA 的私有类,您不能将其转换为任何更具体的类型。
    • .NET Core (macOS):通过 OpenSSL 实现 RSA 的私有类,您不能将其转换为任何更具体的类型。 (这应该在 .NET Core 的下一版本中通过 SecureTransforms)

    【讨论】:

    • 嗨 Barton,你能发布一个在 linux 中使用 OpenSSL 的工作示例代码吗?我使用 RSACryptoServiceProvider 和 CspParameters 在 iss 中部署我的项目以生成 jwt,它运行良好。 Git Repo
    • @MarioRighi 我不确定你要什么。可能您只想问一个不同/更具体的新问题。
    • 我的目标是 .NET Standard 2.0,但rsa.KeySize = desiredKeySizeInBits 没有任何效果?
    • @RosdiKasim 重要的是实际运行时间,而不是目标。 RSA rsa = RSA.Create(); if (rsa is RSACryptoServiceProvider rsaCsp) { rsaCsp.Dispose(); rsa = new RSACng(); } rsa.KeySize = desiredKeySizeInBits; 是一种可能的解决方法。
    • 啊.. 终于我所有的测试都变绿了... 多美啊。谢谢!
    【解决方案2】:

    您将需要改用 dotnetcore 库。将System.Security.Cryptography.Algorithms Nuget 包添加到您的项目中。你可以在这里找到它:System.Security.Cryptography.Algorithms

    它为您的项目提供所有常用算法。

    这是您将用于 Encrypt() 和 Decrypt() 方法的 RSACryptoServiceProvider class

    【讨论】:

    • 我是 .net 核心的新手,我已经通过 Nuget 添加了包。有没有什么地方可以找到这方面的文档,我不使用RSA 类吗? (我以后会考虑更安全的算法,这个任务需要是RSA)。谢谢。
    • 不幸的是,文档确实不多。但我确实设法找到了包含所有课程信息的RSACryptoServiceProvider class reference page。希望这将帮助您实施。
    • 从什么时候开始 RSA 不是一种安全算法,或者更直接地说,您会推荐哪些非对称算法而不是 RSA?此外,OP 从未描述过他的威胁模型,因此它可能非常适合他正在做的事情。
    • 确定与答案无关,删除意见。
    • 我很好奇为什么你说比 RSA 更“安全”的算法会更好?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2017-03-27
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多