Pythonを使用してパスワードジェネレータを作成する方法は?

公開: 2021-08-20

セキュリティは私たちの生活の中で最も重要な部分の1つです。 ほとんどのものがオンラインになっているため、セキュリティの重要性は日々高まっています。 セキュリティについて話すときに、パスワードが明らかになります。

この記事では、ランダムで強力なパスワードをすばやく生成するのに役立つパスワードジェネレータを作成します。

なぜパスワードジェネレータが必要なのですか?

パスワードのさまざまなパターンをすぐに考えることはできないからです。

しかし、それはコンピューターの場合と同じではありません。 コンピューターは、カスタマイズに基づいてランダムで強力なパスワードを数秒で生成できます。 利用可能な多くのパスワードジェネレータがあります。

好きなカスタマイズで自分で作成できますか?

はい、間違いなく作成できます。 そして、ここではその方法を紹介します。

パスワードジェネレータを作成しましょう。

パスワードジェネレータ

独自のパスワードジェネレータを作成することの最も良い点は、好きなようにカスタマイズできることです。

まず、パスワードの長さを尋ね、数字、アルファベット、特殊文字を含むランダムなパスワードを生成するパスワードジェネレータを作成します。

次に、桁数、アルファベット、特殊文字など、各タイプの文字の数を尋ねることで、それを改善します。

それで、これ以上面倒なことはせずに、Pythonを使用してパスワードジェネレータを作成する手順を見てみましょう。

ステップ

  • すべての文字をリストとして保存します。 Pythonの文字列モジュールを使用することも、すべてを入力することもできます。
  • パスワードの長さを入力するようにユーザーに依頼します。
  • 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()

では、前のコードとこのコードの違いは何ですか?

  • パスワードに追加するために、文字の種類ごとに個別のループを作成しました。
  • パスワードの長さで合計文字数を検証するための2つの条件付きチェックがあります。

いくつかのコードを追加しました。 ただし、パターンは前のコードと似ています。 ですから、それを理解するのに何の困難もありません。

次に、出力を実行して確認します。 次のテスト実行を見てください。

 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つのランダムな文字が含まれています。

やあ! 完全に強力なパスワードジェネレータがあります。

結論

パスワードジェネレータを最初から作成する方法を見てきました。 さらに機能を追加できますか? ええ、好きなだけ追加できます。 ただし、結果のパスワードが十分に強力であることを確認してください。

コードからパスワードを生成し、次のオンラインアカウントに使用します。

次に、Pythonで三目並べゲームを作成する方法を確認します。

ハッピーコーディング!