Şifre Oluşturucu "Crapcode"

Sep 27 2020

C # üzerinde Şifre Oluşturucu yaptım .

Bu girmesini ister şifre uzunluğu bu olup, numaralarını içeren , küçük harf , büyük harf veya sembol ve rastgele eklenen sayılar, harfler ya da semboller güçlü bir şifre oluşturur.

Kod var:

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);
                }
            }

        }
    }
}

Are 221 hat bunun için çok fazla? Nasıl empoze edilir? Bu bir "saçmalık" mı? Crapcode nasıl yazılmaz? ....Teşekkürler!

Yanıtlar

7 radarbob Sep 28 2020 at 10:52

Gönderen MSDN Rastgele sınıf belgeleri

Rastgele bir parola oluşturmak için uygun olan gibi şifreleme açısından güvenli bir rastgele sayı oluşturmak için, RNGCryptoServiceProvider sınıfını kullanın veya System.Security.Cryptography.RandomNumberGenerator'dan bir sınıf türetin.


Random Rand = new Random ();

Düzeltmeyi Düzenle : Sistem saatinin anlık görüntüsünü kullanarak, parametresiz yapıcı farklı çekirdek değerleri kullanır; sürece Rastgele nesneleri zamanında çok yakın oluşturulur aynı saat zamanı kullanıldığını. Aynı bağımsız değişken (parametre çekirdek değeri) verildiğinde, farklı Random'lar aynı rastgele diziyi üretir. Düzenle'yi bitir

RandomParametresiz kurucu ile oluşturulan her nesne, her seferinde aynı "varsayılan çekirdek" değeri kullanıldığından, aynı rastgele sayı dizisini üretecektir. Bu, geliştirme ve test için mükemmeldir, ancak üretim kodunda değildir. Sistem süresinin milisaniye cinsinden okunması, her yapıcı çağrısı için farklı "çekirdek" değerler oluşturmanın yaygın bir yoludur.


crapcode Kod Koku

Kodla ilgili belirsiz şüpheleriniz teknik olarak "kod kokusu" olarak adlandırılır, yani herhangi bir nedenle şüpheli görünür - bu durumda uzunluk, yaptığı şey için "uzun" hisseder ". Yanlış bir şeyler olabilir, belki değil; bir "kod kokusu", herhangi bir şeyin gerçekten yanlış olup olmadığını belirlemek için daha yakından incelemeyi gerektirir.

Gördüğüm bir kod kokusu while, her biri farklı gerekli karakterleri arayan çok sayıda benzer görünümlü döngüdür. Daha az satır uğruna anlaşılması zor kod yazmak istemiyorum. "Daha az kod satırı" tek başına bir şeyleri değiştirmek için bir neden değildir. İşte bir fikir, belki bir gelişme olabilir veya olmayabilir:

Kullanıcıya parola sorarken gerekli karakterleri önceden söyleyin.

Bir dize yinelenebilir, "forEach" yapabilirsiniz. A Stringaslında Charteknik olarak bir dizi .

forEach (char character in enteredPassword) { ... }

Her karakteri türüne göre inceleyin. Her karakter türü için ayrı bir sayı tutun.

Testten sonra forEachher sayaç gerekli minimum için. Eğer symbolssıfır, örneğin, ekleme bir hata mesajı "en az bir sembol karakteri gereklidir".


Bilginize

Gerçeğin özü olan eski bir söz vardır: Bir sorunu normal bir ifadeyle çözerseniz, şimdi iki probleminiz var


221 satır bunun için fazla mı

Kod kalitesini değerlendirmek için pek çok kılavuz, ilke, "izm" vb. Vardır. Satır sayısı onlardan biri değil.