using System; using System.Collections.Generic; using System.Text;namespace radam { class Program { static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); } static void Main(string[] args) { string[] a ={"a","b","c","d","e","f","g","h" ,"i","j","k","l","m","n","o","p","q","r" ,"s","t","u","v","w","x","y","z","A","B" ,"C","D","E","F","G","H","I","J","K","L" ,"M","N","O","P","Q","R","S","T","U","V" ,"W","X","Y","Z","0","1","2","3","4","5" ,"6","7","8","9","[","]"}; string s = string.Empty; for (int i = 0; i <8;i++ ) { Random r = new Random(GetRandomSeed()); int j=r.Next(0,63); s = s + a[j];
} Console.WriteLine(s); } }
}
string s = string.Empty; List<string> key = new List<string>(); for (int k=0;k<1000;k++) { for (int i = 0; i < 8; i++) { Random r = new Random(GetRandomSeed()); int j = r.Next(0, 63); s = s + a[j]; } if (!key.Contains(s)) key.Add(s); else s = string.Empty; }
static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); } string s = string.Empty; List<string> key = new List<string>(); whilie(key.Count<8) { for (int i = 0; i < 8; i++) { Random r = new Random(GetRandomSeed()); int j = r.Next(0, 63); s = s + a[j]; } if (!key.Contains(s)) key.Add(s); else s = string.Empty; }
用下面范围内的字符生成八位字符的Key.
A-Z
a-z
0-9
[]像这样的: nGa[rwkH
把Random变量改成静态变量试试.
用foreach循环遍历就好了
using System.Collections.Generic;
using System.Text;namespace radam
{
class Program
{
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
} static void Main(string[] args)
{
string[] a ={"a","b","c","d","e","f","g","h"
,"i","j","k","l","m","n","o","p","q","r"
,"s","t","u","v","w","x","y","z","A","B"
,"C","D","E","F","G","H","I","J","K","L"
,"M","N","O","P","Q","R","S","T","U","V"
,"W","X","Y","Z","0","1","2","3","4","5"
,"6","7","8","9","[","]"};
string s = string.Empty;
for (int i = 0; i <8;i++ )
{
Random r = new Random(GetRandomSeed());
int j=r.Next(0,63);
s = s + a[j];
}
Console.WriteLine(s);
}
}
}
string s = string.Empty;
List<string> key = new List<string>();
for (int k=0;k<1000;k++)
{
for (int i = 0; i < 8; i++)
{
Random r = new Random(GetRandomSeed());
int j = r.Next(0, 63);
s = s + a[j];
}
if (!key.Contains(s))
key.Add(s);
else
s = string.Empty;
}
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
} string s = string.Empty;
List<string> key = new List<string>();
whilie(key.Count<8)
{
for (int i = 0; i < 8; i++)
{
Random r = new Random(GetRandomSeed());
int j = r.Next(0, 63);
s = s + a[j];
}
if (!key.Contains(s))
key.Add(s);
else
s = string.Empty;
}