求两个功能,一是C#随机生成密码,二是在文本框中输入个值,点按钮,生成输入值个数的一些密码和文本框控件再加一个按钮B,点B把生成的密码存入数据库
求两个功能,
一是生成13位随机密码,最好是字母和数字组合,要那种无论生成多少次,多少个,密码都不会有重复的
二是有一个文本框(为了说明详细,我用文本框A,表示这个文本框),一个按钮(按扭A),在文本框(A)中可输入1至20之间的数字,理论上输入数字几都行,但我想在20内,然后点按钮(A),实现生成输入个数的多组控件。
组按件中,包括生成的随机密码,一个文本框(B),和一个按钮(B)。
就是说文本框A中输入1,就生成一个随机密码和一文本框(B),一个按钮。输入10就生成10个随机密码和10个文本框,一个按钮,注意,是就一个按钮(B)就行了,按钮B的功能是,把随机密码和文本框B中再输入的值存入数据库中。
就是说生成10个密码和文本框,点按钮B,就把10个密码和文本框B中再输入的值,存到数据库中,存10行记录。
小弟新人,请误必给出详细的代码和说明,谢谢了
2、你是Web还是Win都不说清楚。。。。。。。思路都一样的
给你一段生成random的代码
public static class RandomTools
{
/// <summary>
/// 生成最大值范围内无重复值的长度为最大值的随机序列,例:6,则返回0,1,2,3,4,5 的List
/// </summary>
/// <param name="maxValue"></param>
/// <returns></returns>
public static List<int> GetRandomList(this int maxValue)
{
if (maxValue == 0)
{
return null;
}
//逻辑描述:生成从0开始到maxValue的tempList
//然后random一次就maxValue--,并将random出来的整数用做索引,加入到returnList并从tempList中移除
maxValue = Math.Abs(maxValue);//防止负数
List<int> tempList = new List<int>();
for (int i = 0; i < maxValue; i++)
{
tempList.Add(i);
}
Random rd = new Random();
List<int> returnList = new List<int>();
while (maxValue > 0)
{
int tempInt = 0;
if (maxValue > 1)//当maxValue为1时,不再进行随机,因为还剩一个数字,无需随机
{
tempInt = rd.Next(maxValue);
}
returnList.Add(tempList[tempInt]);
tempList.RemoveAt(tempInt);
maxValue--;
}
return returnList;
}
/// <summary>
/// 生成指定长度的随机字符串(从字符串资源中)
/// </summary>
/// <param name="length"></param>
/// <param name="source"></param>
/// <returns></returns>
public static string GetRandomString(int length, List<char> source)
{
if (length == 0)
{
return null;
}
StringBuilder strRandom = new StringBuilder();
for (int i = 0; i < length; i++)
{
strRandom.Append(source[Next(source.Count)]);
}
return strRandom.ToString();
}
/// <summary>
/// 生成小于输入值绝对值的随机数
/// </summary>
/// <param name="NumSides"></param>
/// <returns></returns>
public static int Next(this int numSeeds)
{
numSeeds = Math.Abs(numSeeds);
if (numSeeds <= 1)
{
return 0;
}
int length = 4;
if (numSeeds <= byte.MaxValue)
{
length = 1;
}
else if (numSeeds <= short.MaxValue)
{
length = 2;
}
return Next(numSeeds, length);
}
private static int Next(int numSeeds, int length)
{
// Create a byte array to hold the random value.
byte[] buffer = new byte[length];
// Create a new instance of the RNGCryptoServiceProvider.
System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
// Fill the array with a random value.
Gen.GetBytes(buffer);
// Convert the byte to an uint value to make the modulus operation easier.
uint randomResult = 0x0;//这里用uint作为生成的随机数
for (int i = 0; i < length; i++)
{
randomResult
[解决办法]
= ((uint)buffer[i] << ((length - 1 - i) * 8));
}
// Return the random number mod the number
// of sides. The possible values are zero-based
return (int)(randomResult % numSeeds);
}
/// <summary>
/// 生成指定长度的随机字符串(根据枚举资源类型)
/// </summary>
/// <param name="length"></param>
/// <param name="source"></param>
/// <returns></returns>
public static string GetRandomString(int length, RandomSourceType types)
{
List<char> source = GetRandomSource(types);
if (source == null
[解决办法]
source.Count == 0)
{
return null;
}
else
{
return GetRandomString(length, source);
}
}
public static List<char> GetRandomSource(RandomSourceType types)
{
List<char> source = new List<char>();
if (types.HasFlag(RandomSourceType.Number))
{
source.AddPara('0', '9');
}
if (types.HasFlag(RandomSourceType.Lower))
{
source.AddPara('a', 'z');
}
if (types.HasFlag(RandomSourceType.Upper))
{
source.AddPara('A', 'Z');
}
return source;
}
private static void AddPara(this List<char> source, char startChar, char endChar)
{
for (int i = startChar; i <= endChar; i++)
{
source.Add((char)i);
}
}
/*
* gb2312 80
*
* 01-09区为特殊符号。
* 16-55区为一级汉字,按拼音排序。
* 56-87区为二级汉字,按部首/笔画排序。
* 10-15区及88-94区则未有编码。
*
* 所有数字都是从 1 开始
*
* 每个汉字及符号以两个字节来表示。
* 第一个字节称为“高位字节”(也称“区字节)”,upper byte
* 第二个字节称为“低位字节”(也称“位字节”)。low byte
* “高位字节”使用了0xA1-0xF7(把01-87区的区号加上0xA0),
* “低位字节”使用了0xA1-0xFE(把01-94加上 0xA0)。
* 由于一级汉字从16区起始,汉字区的“高位字节”的范围是0xB0-0xF7,
* “低位字节”的范围是0xA1-0xFE,
* 占用的码位是 72*94=6768。
* 其中有5个空位是D7FA-D7FE(55区90-94)
*/
public static string GetRandomPopularSimplifiedChinese(this int length)
{
if (length <= 0)
{
return string.Empty;
}
byte minUpper = 16;
byte maxUpper = 55;
byte rangeLow = 94;
int addParamer = 0xA0;
StringBuilder tempStr = new StringBuilder();
int tempUpper = maxUpper - minUpper + 1;
Encoding gb = Encoding.GetEncoding("gb2312");
for (int i = 0; i < length; i++)
{
int rdUpperValue = Next(tempUpper) + minUpper;
int rdLowValue;
do
{
rdLowValue = Next(rangeLow) + 1;//索引从1开始,所以94种子生成的随机数+1
}
while (rdUpperValue == maxUpper && rdLowValue >= 90);//D7FA-D7FE是空位(55区90-94)
rdUpperValue += addParamer;
rdLowValue += addParamer;
byte[] byteArray = new byte[] { (byte)rdUpperValue, (byte)rdLowValue };
tempStr.Append(gb.GetString(byteArray));
}
return tempStr.ToString();
}
}
[Flags]
public enum RandomSourceType
{
/// <summary>
/// 0-9
/// </summary>
Number = 1,
/// <summary>
/// a-z
/// </summary>
Lower = 2,
/// <summary>
/// A-Z
/// </summary>
Upper = 4
}
[解决办法]
嘿嘿~~~~+1