尝试用c#生成一个简单的密码生成器.这是我目前得到的.试图让控制台打印出用户选择的内容



我似乎不知道如何在用户回答完问题后打印出他们输入的密码。

我想使用4个不同的for循环大写,小写,数字,符号,基于用户输入的内容。如果有人有任何不同的想法,请分享。这将是一个很大的帮助。我是编程新手。

这是我到目前为止写的

string upperCase = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
string lowerCase = ("abcdefghijklmnopqrstuvwxyz");
string numbers = ("1234567890" );
string specChac = ("!@#$%^&*():<>?/");
string randomPassword = upperCase + lowerCase + numbers + specChac;
string allPasswords = "randomPassword";
Random rnd = new Random();
Console.WriteLine("Welocme to the C# password generator! ");
Console.WriteLine("----------------------------------------");
Console.WriteLine("How many uppercase letters would you like in your password ?");
int upperAmount = int.Parse(Console.ReadLine());

Console.WriteLine("How many lowercase letters would you like in your password ?");
int lowerAmount = int.Parse(Console.ReadLine());
Console.WriteLine("How many numbers would you like in your password ?");
int numAmount = int.Parse(Console.ReadLine());
Console.WriteLine("How many special characters would you like in your password?");
int charAmount = int.Parse(Console.ReadLine());

有很多方法来实现它。这张图展示了播种的概念。

没有定义可能的字符,我只是分配了可能的最小和最大ASCII字符代码。
你想怎么做就怎么做。

internal static class RandomPswd
{
const int CHAR_UPPER_MIN = 0x41;
const int CHAR_UPPER_MAX = 0x5a;
const int CHAR_LOWER_MIN = 0x61;
const int CHAR_LOWER_MAX = 0x7a;
const int CHAR_DIGIT_MIN = 0x30;
const int CHAR_DIGIT_MAX = 0x39;
const int CHAR_SYMBL_MIN = 0x21;
const int CHAR_SYMBL_MAX = 0x2f;

private static void Shuffle(char[] buffer)
{
int seed = GetSeed();
Random r = new Random(seed);
int n = buffer.Length;
while (n > 1) {
int k = r.Next(n--);
char t = buffer[n];
buffer[n] = buffer[k];
buffer[k] = t;
}
}

private static int GetSeed()
{
unchecked
{
int hash = (int)DateTime.Now.Ticks * 7302013 ^ Environment.UserName.GetHashCode();
hash = hash * 7302013 ^ (CHAR_UPPER_MIN.GetHashCode());
hash = hash * 7302013 ^ (CHAR_UPPER_MAX.GetHashCode());
hash = hash * 7302013 ^ (CHAR_LOWER_MIN.GetHashCode());
hash = hash * 7302013 ^ (CHAR_LOWER_MAX.GetHashCode());
hash = hash * 7302013 ^ (CHAR_DIGIT_MIN.GetHashCode());
hash = hash * 7302013 ^ (CHAR_DIGIT_MAX.GetHashCode());
hash = hash * 7302013 ^ (CHAR_SYMBL_MIN.GetHashCode());
hash = hash * 7302013 ^ (CHAR_SYMBL_MAX.GetHashCode());
return hash;
}               
}
public static string Create(int upper, int lower, int digits, int symbols)
{
char[] upperChars = new char[upper];
char[] lowerChars = new char[lower];
char[] digitChars = new char[digits];
char[] symblChars = new char[symbols];
int seed = GetSeed();
Random r = new Random(seed);
for (int i=0; i<upper; i++) {
upperChars[i] = (char)r.Next(CHAR_UPPER_MIN, CHAR_UPPER_MAX);
}
seed = GetSeed();
r = new Random(seed);
for (int i = 0; i < lower; i++) {
lowerChars[i] = (char)r.Next(CHAR_LOWER_MIN, CHAR_LOWER_MAX);
}
seed = GetSeed();
r = new Random(seed);
for (int i = 0; i < digits; i++) {
digitChars[i] = (char)r.Next(CHAR_DIGIT_MIN, CHAR_DIGIT_MAX);
}
seed = GetSeed();
r = new Random(seed);
for (int i=0; i< symbols; i++) {
symblChars[i] = (char)r.Next(CHAR_SYMBL_MIN, CHAR_SYMBL_MAX);
}
char[] buf = new char[upper + lower + digits + symbols];
upperChars.CopyTo(buf, 0);
lowerChars.CopyTo(buf, upper);
digitChars.CopyTo(buf, upper + lower);
symblChars.CopyTo(buf, upper + lower + digits);
Shuffle(buf);
return new string(buf);
}
}


static void Main()
{
var pw = RandomPswd.Create(12, 8, 7, 5);
}

相关内容

最新更新