English 中文(简体)
我如何在“C#”方案中产生一套随机示意图,以便它们不会被轻视地预测?
原标题:How do I generate a set of random strings in a C# program so that they are not trivially predicted?

我面临以下问题: 从受限制的字母缩略语中测出独特的字母数字。 我在此在C#中的解决办法:

string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random generator = new Random();
const int ToGenerate = 10000;
const int CharactersCount = 4;
ArrayList generatedStrings = new ArrayList();
while( generatedStrings.Count < ToGenerate ) {
   string newString = "Prefix";
   for( int i = 0; i < CharactersCount; i++ ) {
      int index = generator.Next( Alphabet.Length );
      char character = Alphabet[index];
      newString += character;
   }
   if( !generatedStrings.Contains( newString ) ) {
      generatedStrings.Add( newString );
   }                
}
for( int i = 0; i < generatedStrings.Count; i++ ) {
    System.Console.Out.WriteLine( generatedStrings[i] );
}

它产生10K座标,从“固定”开始,或以其他方式由英文大写字母和号码组成。 产出看好。

现在我看到以下问题。 生产的胎体是不可能由任何人预测的。 在我的方案中,种子依赖时间。 一旦有人知道种子价值,他就能够操作同样的代码,并获得同样的指示。 如果他知道任何两条插图,他可以很容易地读出我的算法(因为它确实是阴险的),并试图淡化种子价值——只是列举所有可能的种子价值,直到他看到产出中两个已知的插图。

我的法典是否可以作一些简单的改动,以降低所描述袭击的可能性?

最佳回答

谁知道种子? 除非他知道exact time,否则你就掌握了该守则,即very难以做到。 但是,如果你需要更强大的话,你也可以通过<编码>System. Security.Cryptography.RandomNumberGenerator.Create——例如:

        var rng = System.Security.Cryptography.RandomNumberGenerator.Create();
        byte[] buffer = new byte[4];
        char[] chars = new char[CharactersCount];
        for(int i = 0 ; i < chars.Length ; i++)
        {
            rng.GetBytes(buffer);
            int nxt = BitConverter.ToInt32(buffer, 0);
            int index = nxt % Alphabet.Length;
            if(index < 0) index += Alphabet.Length;
            chars[i] = Alphabet[index];
        }
        string s = new string(chars);
问题回答

但这取决于你认为的“简单”。

你可以通过使用随机数字的“真实”来源来“解决”你的问题。 你们可以尝试自由选择(random.org、4米拉布热点等)或

或者(也许更好)不是预先产生,而是按需求产生。 但这可能是对你的业务流程/模式的重大改变。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签