.Net Core 中的 RNGCryptoServiceProvider [英] RNGCryptoServiceProvider in .Net Core

查看:40
本文介绍了.Net Core 中的 RNGCryptoServiceProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NetCore 中找不到 System.Security.Cryptography.RNGCryptoServiceProvider 类.

I cannot find the System.Security.Cryptography.RNGCryptoServiceProvider class in .NetCore.

这对于我尝试从 .Net Framework 移植的应用程序至关重要,因为它用于生成用于加密的初始化向量.

It is essential to the application I am trying to port from .Net Framework, as it is being used to generate an initialisation vector for encryption.

它是否以不同的名称存在,或者是否有其他方法可以实现此功能?

Does it exist under a different name, or is there another way of achieving this functionality?

推荐答案

System.Security.Cryptography.RandomNumberGenerator 是加密安全伪随机数生成器 (CSPRNG) 实现的基类.在 .NET Framework RandomNumberGenerator.Create() 返回一个 RNGCryptoServiceProvider 实例(除非通过 CryptoConfig).在 .NET Core 中,RandomNumberGenerator.Create() 返回一个基于 BCryptGenRandom (Windows) 或 OpenSSL 的随机数生成器 (!Windows) 的不透明类型.

System.Security.Cryptography.RandomNumberGenerator is the base class for Cryptographically-Secure Pseudo-Random Number Generator (CSPRNG) implementations. In .NET Framework RandomNumberGenerator.Create() returns an RNGCryptoServiceProvider instance (unless configured differently by CryptoConfig). In .NET Core RandomNumberGenerator.Create() returns an opaque type which is based on BCryptGenRandom (Windows) or OpenSSL's random number generator (!Windows).

RandomNumberGenerator.Create() 是在 .NET Core 上获取 RNG 实例的唯一方法,因为它适用于 .NET Core 和 .NET Framework,因此是最便携的.

RandomNumberGenerator.Create() is the only way to get an RNG instance on .NET Core, and since it works on both .NET Core and .NET Framework is the most portable.

当然,如果你正在生成一个 IV,你也可以只调用实例方法 SymmetricAlgorithm.GenerateIV() 让它在内部使用 CSPRNG;尽管正如文档所说,调用它是不必要的,因为实例创建了一个随机 IV(GenerateIV 可用于强制它在下一次调用 CreateEncryptor 之前生成一个新的代码>).

Of course, if you're generating an IV, you can also just call the instance method SymmetricAlgorithm.GenerateIV() to have it use the CSPRNG internally; though as the documentation says, calling it is unnecessary as a random IV is created with the instance (GenerateIV can be used to force it to generate a new one before the next call to CreateEncryptor).

这篇关于.Net Core 中的 RNGCryptoServiceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆