Python을 사용하여 암호 생성기를 만드는 방법은 무엇입니까?
게시 됨: 2021-08-20보안은 우리 삶의 가장 중요한 부분 중 하나입니다. 대부분의 일이 온라인으로 진행됨에 따라 보안의 중요성이 날로 증가하고 있습니다. 암호는 보안에 대해 이야기할 때 밝혀집니다.
이 기사에서는 임의의 강력한 암호를 빠르게 생성하는 데 도움이 되는 암호 생성기를 만들 것입니다.
비밀번호 생성기가 필요한 이유는 무엇입니까?
다른 패턴의 비밀번호를 즉시 생각할 수 없기 때문입니다.
그러나 컴퓨터의 경우는 그렇지 않습니다. 컴퓨터는 몇 초 만에 사용자 정의에 따라 임의의 강력한 암호를 생성할 수 있습니다. 사용할 수 있는 암호 생성기가 많이 있습니다.
우리가 좋아하는 커스터마이징으로 우리만의 것을 만들 수 있습니까?
예, 확실히 만들 수 있습니다. 그리고 여기에서 그 방법을 알려드리겠습니다.
암호 생성기를 만들어 봅시다.
비밀번호 생성기
자체 암호 생성기를 만드는 가장 좋은 점은 원하는 대로 사용자 지정할 수 있다는 것입니다.
먼저 암호의 길이를 묻고 숫자, 알파벳 및 특수 문자가 포함된 임의의 암호를 생성하는 암호 생성기를 만듭니다.
다음으로 자릿수, 알파벳, 특수문자 등 각 유형의 문자 개수를 물어보면서 개선해 보겠습니다.
따라서 더 이상 고민하지 않고 Python을 사용하여 암호 생성기를 만드는 단계를 살펴보겠습니다.
단계
- 모든 문자를 목록으로 저장합니다. Python의 string 모듈을 사용하거나 모두 입력할 수 있습니다.
- 사용자에게 암호 길이를 입력하도록 요청합니다.
-
random.shuffle메소드를 사용하여 문자를random.shuffle. - 비밀번호를 저장할 빈 목록을 초기화하십시오.
-
length반복하는 루프를 작성하십시오.-
random.choice메서드를 사용하여 모든 문자에서 임의의 문자를 선택합니다. - 암호에 임의의 문자를 추가합니다.
-
- 결과 암호 목록을 섞어서 더 무작위로 만듭니다.
-
join방법을 사용하여 암호 목록을 문자열로 변환합니다. - 비밀번호를 인쇄합니다.
위의 단계에 따라 코드를 작성해 보십시오. 코드를 작성할 수 없더라도 걱정하지 마십시오. 아래 코드를 확인하세요.
암호
import string import random ## characters to generate password from characters = list(string.ascii_letters + string.digits + "[email protected]#$%^&*()") def generate_random_password(): ## length of password from the user length = int(input("Enter password length: ")) ## shuffling the characters random.shuffle(characters) ## picking random characters from the list password = [] for i in range(length): password.append(random.choice(characters)) ## shuffling the resultant password random.shuffle(password) ## converting the list to string ## printing the list print("".join(password)) ## invoking the function generate_random_password()위의 코드는 자명합니다. 코드 작성에 대해 설명된 단계를 따랐습니다. 따라서 단계를 읽으면 코드를 이해하는 데 문제가 없습니다.
이제 코드를 실행하고 암호를 생성하십시오. 예제 출력은 다음과 같습니다.
Enter password length: 10 d&amIzK%d)위의 출력에서 암호를 확인하십시오. 숫자가 있습니까? 아니요. 프로그램에 숫자가 포함된다는 보장이 없기 때문입니다.
다음으로 사용자에게 원하는 숫자, 알파벳 및 특수 문자를 입력하도록 요청하여 프로그램에 숫자와 특수 문자가 포함되도록 합니다.
사용자가 각 유형의 문자 수를 입력하면 프로그램은 해당 문자 유형 수를 암호에 포함합니다.
각 유형의 문자 수를 허용하고 암호를 생성하는 코드를 살펴보겠습니다.
import string import random ## characters to generate password from alphabets = list(string.ascii_letters) digits = list(string.digits) special_characters = list("[email protected]#$%^&*()") characters = list(string.ascii_letters + string.digits + "[email protected]#$%^&*()") def generate_random_password(): ## length of password from the user length = int(input("Enter password length: ")) ## number of character types alphabets_count = int(input("Enter alphabets count in password: ")) digits_count = int(input("Enter digits count in password: ")) special_characters_count = int(input("Enter special characters count in password: ")) characters_count = alphabets_count + digits_count + special_characters_count ## check the total length with characters sum count ## print not valid if the sum is greater than length if characters_count > length: print("Characters total count is greater than the password length") return ## initializing the password password = [] ## picking random alphabets for i in range(alphabets_count): password.append(random.choice(alphabets)) ## picking random digits for i in range(digits_count): password.append(random.choice(digits)) ## picking random alphabets for i in range(special_characters_count): password.append(random.choice(special_characters)) ## if the total characters count is less than the password length ## add random characters to make it equal to the length if characters_count < length: random.shuffle(characters) for i in range(length - characters_count): password.append(random.choice(characters)) ## shuffling the resultant password random.shuffle(password) ## converting the list to string ## printing the list print("".join(password)) ## invoking the function generate_random_password()그렇다면 이전 코드와 이 코드의 차이점은 무엇입니까?

- 암호에 추가하기 위해 각 유형의 문자에 대해 별도의 루프를 작성했습니다.
- 암호 길이로 총 문자 수를 확인하는 두 가지 조건부 검사가 있습니다.
몇 가지 추가 코드를 추가했습니다. 그러나 패턴은 이전 코드와 유사합니다. 따라서 이해하는 데 어려움이 없을 것입니다.
이제 출력을 실행하고 확인할 시간입니다. 다음 테스트 실행을 보십시오.
Enter password length: 10 Enter alphabets count in password: 3 Enter digits count in password: 2 Enter special characters count in password: 3 V2(&#XlQq1이번에 생성된 비밀번호를 보면 사용자가 원하는 최소한의 글자수를 가지고 있는 것이다. 그리고 이 프로그램에는 사용자 입력과 동일한 암호 길이를 만들기 위해 2개의 임의의 문자가 더 포함되어 있습니다.
만세! 우리는 완전하고 강력한 암호 생성기를 가지고 있습니다.
결론
암호 생성기를 처음부터 만드는 방법을 살펴보았습니다. 더 많은 기능을 추가할 수 있습니까? 예, 원하는 만큼 추가할 수 있습니다. 그러나 결과 암호가 충분히 강력한지 확인하십시오.
코드에서 암호를 생성하고 다음 온라인 계정에 사용하십시오.
다음으로 파이썬으로 틱택토 게임을 만드는 방법을 알아보세요.
행복한 코딩!
