무차별 암호 대입

Aug 21 2020

하루 전에 암호를 무차별 대입하는 AC # 프로젝트를 시작했습니다. 암호는 정수와 문자열이 될 수 있습니다. 코드에서 비밀번호의 문자 수를 확인합니다. 이것이 속임수이지만 그렇지 않으면 크랙하는 데 너무 오래 걸립니다. 나는 그것이 충분히 좋은지 확인하기 위해 여기에 게시하고 있습니다.

using System;

namespace Hacking_Project
{
    class Program
    {
        static void Main()
        {
            //Console Color
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            //If password is found
            bool done = false;
            //If the password is a string, not the best name but ok
            bool yes = false;
            //If the Guessed password is the same number of characters as the original password
            int pass = 0;
            //Guessed password
            string pass_check = "";
            //Possible characters for the password
            char[] pos = {'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'};
            Console.WriteLine("What is the password?");
            //Asking for the password to crack
            string password = Console.ReadLine();
            //How much characters does the password has. I know that this is kinda cheating, but then the bruteforce would take to much
            int digits = password.Length;
            //Initialising the random number generator
            Random rand = new Random();
            //The choices that the calculator will take for the guessed password(int)
            int[] choices = new int[digits];
            //The choices that the calculator will take for the guessed password(string)
            char[] choices1 = new char[digits];
            
            //If the password is a string
            for (int i = 0; i < pos.Length; i++)
            {
                if (password.Contains(pos[i]))
                {
                    password = password.ToLower();
                    yes = true;
                }
            }
            //The Cracking Part
            while (done == false)
            {
                if (!yes)
                {
                    for (int i = 0; i < digits; i++)
                    {
                        choices[i] = rand.Next(0, 9);
                        pass_check += choices[i];
                        pass++;
                        //Console Color
                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                        if (pass != digits)
                        {
                            Console.Write(choices[i]);
                        }
                        else
                        {
                            Console.Write(choices[i] + ", ");
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < digits; i++)
                    {
                        choices1[i] = pos[rand.Next(pos.Length)];
                        pass_check += choices1[i];
                        pass++;
                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                        if (pass != digits)
                        {
                            Console.Write(choices1[i]);
                        }
                        else
                        {
                            Console.Write(choices1[i] + ", ");
                        }
                    }
                }
                pass = 0;
                if (pass_check == password)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("\n\nThe password is: ");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(pass_check);

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("\n\nThe original password is: ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(password);

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("\n\nDo you want to restart?");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write(" => ");
                    string restart = Console.ReadLine();
                    restart = restart.ToLower();

                    if (restart == "yes")
                    {
                        Main();
                    }
                    else if (restart == "no")
                    {
                        done = true;
                    }
                }
                //If the password is not found, quessed password is set to empty
                else
                {
                    pass_check = "";
                }
            }
        }
    }
}

답변

2 MaLiN2223 Aug 23 2020 at 05:20

면책 조항 : 아래는 제 의견 일뿐입니다. 진실의 원천으로 취급하지 마십시오. 또한 코드가 예상대로 정확히 작동한다고 가정합니다. 성능이나 유효성에 대해서는 생각하지 않을 것입니다.

  • 주석이 너무 많으면 대신 코드를 더 설명하기 위해 리팩토링하십시오. 밥 삼촌이 가장 잘 말했다 : '코멘트는 코드로 자신을 표현하지 못하는 것'(물론 설명 할 수없는 '왜'가 아니라면).
  • 소규모 및 단일 책임 함수 / 클래스에 대한 코드를 개발하십시오 . 지침 은 여기 를 참조 하십시오 .
  • 콘솔 관련 작업은 별도의 클래스 (래퍼)에 위임 할 수 있으므로 향후 다른 소스의 입력 / 출력을 처리하도록 쉽게 확장 할 수 있습니다. 또한 중복을 방지하기 위해 (예 : \n\n문자열의 infront 작성을 추상화 할 수 있음).
  • 변수 이름은 내용을 나타내야합니다 (매우 분명하지 않은 경우). 당신은 '최고의 이름은 아니지만 괜찮아'라는 댓글을 썼습니다. 그렇습니다. 그것은 최고의 이름은 아니지만 나는 그것이 'OK'라고 생각하지 않습니다. 이 변수가 의미하는 바를 확인하기 위해 코드를 반쯤 스크롤해야했습니다. 이것은 옳지 않습니다. 유사 같은 변수에 간다 choices1, digits, pos등이있다.
  • while (done == false)수 while(!done)는 모두 짧고 '영어'더 설명입니다.
  • Console.WriteLine추가하는 대신 사용하십시오 \n\n( Console.WriteLine("").
  • 에 대한 재귀 대신 while 루프를 사용하십시오 Main. 두 가지 이유 : 스택을 초과 할 수 있으며 (인간에게는 좋지 않지만 모든 봇이 쉽게 수행 할 수 있음) 다른 사용자 가 재귀 함수보다 게임 루프 를 보게 될 것으로 예상됩니다 .
  • 다시 시작할 수있는 입력 값을 표시합니다. 사용자가을 시도해도 Y작동하지 않습니다.