简体   繁体   English

C# 中的 SecureRandom

[英]SecureRandom in C#

Here is the java code:这是Java代码:

SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(someBytes);//someBytes is the seed

Is there any equal method in C#? C# 中是否有任何相等的方法? What I have get is not correct:我得到的是不正确的:

RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
rng.GetBytes(someBytes);// out someBytes

I do need the seed, because the java code did, I have to translate the java code into C#.我确实需要种子,因为java代码需要,我必须将java代码翻译成C#。 When I pass the same seed, the sequence I get from C# must equal with the java.当我传递相同的种子时,我从 C# 得到的序列必须与 java.lang.String 相同。

The abstract class System.Security.Cryptography.RandomNumberGenerator and its concrete implementations do not expose a method for setting a seed to the developer (though internally, I suspect they do in fact use one.) 抽象类System.Security.Cryptography.RandomNumberGenerator及其具体实现不公开将种子设置给开发人员的方法(虽然在内部,我怀疑他们确实使用了一个。)

The design rationale there was, I suspect, that repeatability does not make for a 'cryptographically strong' stream of random values. 我怀疑,设计的基本原理是,可重复性不会产生“密码强”的随机值流。

If you look at the concrete implementation, RNGCryptoServiceProvider , while it does expose a constructor accepting a byte[] to presumably initialize the PRNG, its documentation says 如果你看一下具体的实现, RNGCryptoServiceProvider ,虽然它确实暴露了一个接受一个byte[]的构造函数来推测初始化PRNG,但它的文档说

This value is ignored. 该值被忽略。

And the remarks go on to say 这些评论继续说

This method does not directly initialize the RNGCryptoServiceProvider class. 此方法不直接初始化RNGCryptoServiceProvider类。 Calling this method is equivalent to calling the RNGCryptoServiceProvider constructor and passing null . 调用此方法等同于调用RNGCryptoServiceProvider构造函数并传递null

For information on the sort of stuff that goes into the seed that's used, see the MSDN documentation for CryptGenRandom 有关使用种子的东西的信息,请参阅CryptGenRandom的MSDN文档

According to the MSDN docs for RNGCryptoServiceProvider there doesn't appear to be a way to manually seed it with values yourself. 根据RNGCryptoServiceProvider的MSDN文档,似乎没有办法用自己手动为其RNGCryptoServiceProvider There are constructors that take a byte[] and string , but both of those arguments are ignored . 有一些构造函数接受byte[]string ,但这两个参数都被忽略

This doesn't matter, because any random number generator worth its weight in salt will properly seed itself upon creation. 这没关系,因为任何在盐中值得重量的随机数发生器都会在创建时适当地播种。 Any value you provide is unlikely to be any better than the internal seeding mechanism (which is probably a high-resolution time-derived value). 您提供的任何值都不可能比内部播种机制(可能是高分辨率的时间导出值)更好。

RNGCryptoServiceProvider类不需要手动播种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM