암호 생성기“Crapcode”
C #에서 암호 생성기 를 만들었습니다 .
숫자 , 소문자 , 대문자 또는 기호를 포함 하는지 여부에 관계없이 사용자에게 비밀번호 길이를 묻는 메시지를 표시하고 임의로 추가 된 숫자, 문자 또는 기호에서 강력한 비밀번호를 생성합니다.
코드가 있습니다.
using System;
namespace Password_Generator
{
class Program
{
static string Randomize(string Str)
{
Random Rand = new Random();
string Randomized = "";
while (Randomized.Length < Str.Length)
{
Randomized += Str[Rand.Next(0, Str.Length)];
}
return Randomized;
}
static void Main(string[] args)
{
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nDo Yout Want a New Password [True], [False]? ");
Console.ForegroundColor = ConsoleColor.Yellow;
string NewPasswordStr = Console.ReadLine();
bool NewPassword;
while (bool.TryParse(NewPasswordStr, out NewPassword) == false)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Given Output is Unconvertable Or it's Value is Unacceptable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nDo Yout Want a New Password [True], [False]? ");
Console.ForegroundColor = ConsoleColor.Yellow;
NewPasswordStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
if (NewPassword == true)
{
Console.Clear();
Console.Write("Password Length: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string LenghtStr = Console.ReadLine();
int Lenght;
while (int.TryParse(LenghtStr, out Lenght) == false || int.Parse(LenghtStr) <= 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The Given Output is Unconvertable Or it's Value is Unacceptable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nPassword Length: ");
Console.ForegroundColor = ConsoleColor.Yellow;
LenghtStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Numbers (0 - 9) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeNumbersStr = Console.ReadLine();
bool IncludeNumbers;
while (bool.TryParse(IncludeNumbersStr, out IncludeNumbers) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Numbers (0 - 9) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeNumbersStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write("\n\nInclude Lowercase Letters (a - z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeLowercaseStr = Console.ReadLine();
bool IncludeLowercase;
while (bool.TryParse(IncludeLowercaseStr, out IncludeLowercase) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Lowercase Letters (a - z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeLowercaseStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Uppercase Letters (A - Z) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeUppercaseStr = Console.ReadLine();
bool IncludeUppercase;
while (bool.TryParse(IncludeUppercaseStr, out IncludeUppercase) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Uppercase Letters (A - X) [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeUppercaseStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\nInclude Symbols [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
string IncludeSymbolsStr = Console.ReadLine();
bool IncludeSymbols;
while (bool.TryParse(IncludeSymbolsStr, out IncludeSymbols) == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nThe Given Output is Unconvertable. Try Again!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nInclude Symbols [True], [False]: ");
Console.ForegroundColor = ConsoleColor.Yellow;
IncludeSymbolsStr = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
string Password = "";
while (Password.Length < Lenght)
{
Random Rand = new Random();
if (IncludeNumbers == true)
{
Password += ((char)Rand.Next(48, 58)).ToString();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeLowercase == true)
{
Password += ((char)Rand.Next(97, 123)).ToString();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeUppercase == true)
{
Password += ((char)Rand.Next(97, 123)).ToString().ToUpper();
}
if (Password.Length == Lenght)
{
break;
}
if (IncludeSymbols == true)
{
Password += ((char)Rand.Next(33, 48)).ToString().ToUpper();
}
if (Password.Length == Lenght)
{
break;
}
}
Password = Randomize(Password);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(Password);
Console.ForegroundColor = ConsoleColor.White;
}
else
{
Environment.Exit(-1);
}
}
}
}
}
인가 221 라인이 너무 많이? 그것을 자극하는 방법? 이것이 "크랩 코드"입니까? crapcode를 작성하지 않는 방법은 무엇입니까? ....감사!
답변
에서 MSDN 임의의 클래스 문서
임의 암호를 만드는 데 적합한 것과 같이 암호화 보안 난수를 생성하려면 RNGCryptoServiceProvider 클래스를 사용하거나 System.Security.Cryptography.RandomNumberGenerator에서 클래스를 파생합니다.
랜덤 랜드 = new Random ();
수정 수정 : 시스템 시계의 스냅 샷을 사용하여 매개 변수가없는 생성자는 다른 시드 값을 사용합니다. Random 개체가 너무 가깝게 만들어 져서 동일한 시계 시간이 사용 되지 않는 한 . 동일한 인수 (매개 변수 시드 값)가 주어지면 서로 다른 랜덤이 동일한 랜덤 시퀀스를 생성합니다. 끝 편집
매번 Random동일한 "기본 시드"값이 사용되기 때문에 매개 변수없는 생성자로 생성 된 모든
객체는 동일한 난수 시퀀스를 생성합니다. 이것은 개발 및 테스트에 적합하지만 프로덕션 코드에는 적합하지 않습니다. 시스템 시간을 밀리 초 단위로 읽는 것은 각 생성자 호출에 대해 서로 다른 "시드"값을 생성하는 일반적인 방법입니다.
crapcode
코드 냄새
코드에 대해 가지고있는 모호한 의심을 기술적으로 "코드 냄새"라고합니다. 이것은 어떤 이유로 의심스러워 보입니다.이 경우에는 길이가 "느낌"이 길어 보입니다. 뭔가 잘못되었을 수도 있고 아닐 수도 있습니다. "코드 냄새"는 실제로 잘못된 것이 있는지 확인하기 위해 면밀한 조사가 필요합니다.
내가 본 코드 냄새는 while각각 다른 필수 문자를 찾는 여러 개의 유사한 루프입니다. 줄 수가 적기 때문에 이해하기 어려운 코드를 작성하고 싶지 않습니다. "더 적은 코드 줄"만으로는 상황을 바꿀 수 없습니다. 여기에 아이디어가 있습니다. 개선 일 수도 있고 아닐 수도 있습니다.
사용자에게 암호를 입력 할 때 필요한 문자를 미리 알려줍니다.
문자열은 반복 가능하며 "forEach"를 사용할 수 있습니다. A String는 사실 Char기술적으로 의 배열입니다 .
forEach (char character in enteredPassword) { ... }
각 문자의 유형을 조사하십시오. 각 문자 유형에 대해 별도의 수를 유지하십시오.
forEach필요한 최소값에 대해 각 카운터를 테스트 한 후 . 경우 symbols제로, 예를 들어, APPEND 오류 메시지에 "적어도 하나 개의 기호 문자가 필요합니다."
참고로
진실의 핵심이 담긴 오래된 속담 이 있습니다. 정규 표현식으로 문제를 해결하면 이제 두 가지 문제가 있습니다
221 라인이 너무 많습니까?
코드 품질을 평가하기위한 많은 지침, 원칙, "isms"등이 있습니다. 줄 수는 그중 하나가 아닙니다.