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

查看:128
本文介绍了.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 可用于强制它在下一次调用时生成一个新的IV。 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天全站免登陆