Why this matters
Almost every online account you have is protected by a password. Your email, your bank, your code hosting account and your phone's app store all ask for one. Attackers know this, so they try to get your password in three main ways.
They guess. A program tries common words, names and short strings until one works. They reuse. They take a password leaked from one website and try it on your email and bank, because many people use the same password everywhere. And they steal. They send you a fake login page and wait for you to type your password into it.
This lesson covers the first two. The lesson in this module, "Recognising phishing and scams", covers the third. Two-factor login, which adds a second check after the password, is covered in the lesson "Two-factor login and passkeys".
Good password habits stop most of these attacks with no special tools. The habits are simple to state: use long passphrases, use a different password for each site, and store them in a password manager. The rest of this lesson shows why each habit works, with numbers you can check yourself.
The idea: a password is a guess that nobody can make
A password works because the site expects you to know something that a stranger does not. The site compares what you type with a stored copy and lets you in if they match. A weak password is one that a stranger can guess before you can finish reading this paragraph.
Think of a lock with a combination of four digits. There are 10 times 10 times 10 times 10 combinations, which is 10,000. A person trying every combination by hand would need a long time. A computer tries them in a fraction of a second. Now think of a lock with eight digits. There are 100 million combinations, and a computer still tries them quickly, but the number is far larger.
Security people measure this with a unit called bits of entropy. Entropy here means how many different possible passwords the method could have produced. Each extra bit doubles the number of guesses a stranger has to make. The formula is simple:
- Take the number of possible characters or words you could pick from. Call it N.
- Each pick adds log2(N) bits. The number log2(N) answers the question "how many times must I double 2 to reach N". For example, log2(8) is 3, because 2 times 2 times 2 is 8.
- Multiply by the number of picks.
A password from a random process has known entropy, because you know how it was made. A password you invented in your head usually has much less entropy than it feels like, because people choose patterns: a name, a year, a word followed by a number, or a keyboard row.
Worked example 1: why "Summer2026!" is weak
Suppose you choose a password like Summer2026!. It has 11 characters, uses upper case, lower case, digits and a symbol, and it looks complicated. But a guessing program does not try random strings. It tries words from a list of common words, then adds years, then adds common symbols at the end. This pattern is so common that a guessing program tries it early.
The problem is not the length or the mix of characters. The problem is that the choice was not random. The program only has to guess the word "Summer", the year range, and the symbol. Each of those is a small set of options.
The lesson from this example is that a password is strong because of how it was made, not because it looks complicated. A random password of 12 characters from a large set has far more entropy than a human-made password of 11 characters with a pattern.
Long passphrases
A passphrase is a password made of several words joined together. An example is forest-candle-meadow-kettle. It is easy to type and easy to remember, and it is long.
The strength of a passphrase comes from the word list you pick the words from. A common method is the Diceware list, which has 7,776 short words. The number 7,776 is 6 to the power of 5, because the list was designed so that five dice rolls pick one word. Each word gives you about 12.9 bits of entropy, because log2(7776) is about 12.92.
Here is a short Python program that shows the arithmetic. The program uses a 16-word list so the output is short. A real passphrase needs a real list of thousands of words, as described above.
import math
import secrets
import string
# A 16-word list for the demo. A real diceware list has 7776 words.
demo_words = ["apple", "bridge", "candle", "daisy", "engine", "forest", "garden", "harbor",
"island", "jacket", "kettle", "ladder", "meadow", "needle", "orchid", "pebble"]
print("demo list size:", len(demo_words))
print("bits per word from demo list:", round(math.log2(len(demo_words)), 2))
print("bits per word from 7776-word list:", round(math.log2(7776), 2))
print("6 words from 7776:", round(6 * math.log2(7776), 1), "bits")
print("4 words from demo list:", round(4 * math.log2(len(demo_words)), 1), "bits")
print("12 random characters from letters, digits, symbols:",
round(12 * math.log2(len(string.ascii_letters + string.digits + string.punctuation)), 1), "bits")
print("14 random lowercase letters:", round(14 * math.log2(26), 1), "bits")
print("passphrase from demo list:", "-".join(secrets.choice(demo_words) for _ in range(4)))
print("random 16 char password:", "".join(secrets.choice(string.ascii_letters + string.digits) for _ in range(16)))Run it with python3 passwords.py. The output from one run was:
demo list size: 16
bits per word from demo list: 4.0
bits per word from 7776-word list: 12.92
6 words from 7776: 77.5 bits
4 words from demo list: 16.0 bits
12 random characters from letters, digits, symbols: 78.7 bits
14 random lowercase letters: 65.8 bits
passphrase from demo list: forest-candle-meadow-kettle
random 16 char password: oq3Rq3p2u0T2l24sYour last two lines will differ, because the program picks new random values every time. The numbers above them do not change.
Look at the results. Four words from the 16-word demo list give only 16 bits, which is weak. Six words from the 7,776-word list give 77.5 bits, which is strong. Twelve random characters from the full set of letters, digits and symbols give 78.7 bits. A passphrase of six real words and a random password of twelve characters are about equally strong, and the passphrase is much easier to type.
The program uses the secrets module on purpose. Python has a second module called random that also picks values. Python's documentation says that random is not suitable for security purposes, because its output can be predicted once someone sees enough of it. The secrets module uses the operating system's source of random numbers, which is designed to be unpredictable.
Worked example 2: building a passphrase by hand
You do not need Python to make a passphrase. If you have a list of words and a dice, you can do it on paper:
- Roll a six-sided die five times and write the five numbers in a row, for example 3, 1, 4, 2, 6.
- Look up the number 31426 in the Diceware list. The list gives each five-digit number a word.
- Repeat until you have six words.
- Join the six words with a symbol you can type, such as a hyphen.
This gives a passphrase with about 77 bits of entropy. The words are random, so nobody can guess them from your name or your habits. You can read the passphrase out loud and type it without looking at the screen.
The rule for picking words is simple: the words must be chosen by a random process, not by you. Choosing your own six words feels random, but people tend to choose words that are connected to their life, such as a pet's name and a favourite place. Those choices are much easier to guess.
Worked example 3: a passphrase for a bank
Suppose you need a new password for a bank. The bank accepts passwords of up to 64 characters and does not accept spaces. You want a passphrase with at least 60 bits of entropy.
Six words from the 7,776-word list give 77.5 bits. Five words give about 64.6 bits, which is enough. Join them with hyphens, which is a symbol the bank may accept. The result looks like ladder-harbor-jacket-kettle-orchid. It is 34 characters long, which is under the 64-character limit.
Check the rules of each site before you choose the format. Some sites refuse spaces or certain symbols, and some set a maximum length. A passphrase that the site rejects has no value. If a site rejects a good passphrase, do not weaken your choice. Pick a different symbol or a shorter count of words that still meets your target.
One password per site
A passphrase is only half of the habit. The other half is using a different password on each site.
Here is why. Websites store your password in a database. The database can be stolen, and this happens often. When a site is breached, the stolen list of email addresses and passwords is sold or shared. Attackers then try each pair on other sites, because they know many people reuse passwords. This is called credential stuffing. It works automatically, and it does not need any cleverness.
Worked example: one breach, three accounts
Suppose you use forest-candle-meadow-kettle on a shopping site, your email and your bank. The shopping site is breached. The attacker now has your email address and that passphrase. They try the passphrase on your email. It works. They then reset the bank password through your email. Three accounts are now in danger, and the first breach was on a site you did not think was important.
Now suppose you used a different passphrase on each site. The shopping site breach gives the attacker one passphrase. It works only on the shopping site, because nowhere else uses it. The email and bank passwords were never in the stolen list, so the attacker cannot reach them.
The same logic applies to any site that holds a password, including small forums and old accounts you no longer use. A forgotten account can still be breached, and its password can still be tried elsewhere.
Using a password manager
If you use a different passphrase on every site, you will have dozens or hundreds of them. Nobody can remember that many. A password manager is a program that creates strong passwords, stores them in an encrypted file, and fills them into login forms for you.
Here is what a password manager does, in plain terms:
- It keeps a vault. The vault is a file that holds your passwords, and it is encrypted, which means the contents are scrambled so that nobody without the key can read them.
- It is unlocked by one master password. This is the only password you need to remember. It opens the vault, and the manager uses it to produce the key that unscrambles the contents.
- It creates new passwords. When you sign up for a site, the manager can generate a long random password and save it in the vault in the same step.
- It fills in forms. When you visit a site, the manager finds the saved entry for that site and fills in the username and password. Good managers match the entry to the site's exact address, so they will not fill a password into a page on a different address.
A password manager does not make a weak password strong, and it does not protect you from every attack. It protects you from reuse and from guessing, because the passwords it creates are long and random. It also removes the need to type passwords, which makes it less likely that you type one into a fake site, because the manager will not fill it in there.
What the master password must be
Your master password is the one password that opens everything. It should be a long passphrase made from random words, as described above. It must be one you do not use anywhere else, and it must be one you can type from memory.
Keep a copy of the master password written down, in a safe place you control, such as a locked drawer at home. If you lose the master password, most managers cannot recover your vault. The company that makes the manager usually holds no copy of it, because the design is that only you hold the key. This is good for privacy and bad if you forget. A written backup solves that problem without weakening the design.
Setting up a manager, step by step
The steps are similar in most managers:
- Pick a manager. Choose one from a company you trust, and check whether it offers a free plan, a paid plan or both. Read what it stores and whether it syncs between devices.
- Install the app or browser extension on your computer and phone.
- Create the vault with your master passphrase. Write the master passphrase on paper and store it safely.
- Turn on the manager's option to show a warning when a password is weak or reused. Many managers also check passwords against lists of breached passwords.
- Change your most important passwords first: email, bank, and the account where you store the manager's own login. Your email is the most important, because password reset emails go there.
- Change the rest as you log in to each site. Use the manager's generator to create a new password, and let it save the change.
Do not change everything in one evening. It takes time, and some sites will ask you to log in again. Work through the list over a few weeks, and start with the accounts that would do the most harm if someone broke in.
Common mistakes
Using the same passphrase with a number changed. Changing forest-candle-meadow-kettle to forest-candle-meadow-kettle2 does not make a new password. Guessing programs try a number at the end as a matter of course. A different passphrase needs different random words.
Choosing the words yourself. Words you pick are not random. People pick words that they like, that relate to their work or family, or that they see on the screen. Use dice, a random generator, or the manager's generator.
Writing passwords on a sticky note by the computer. A written password is not a problem in itself. A written password next to the computer, or in a file named "passwords.txt" on the desktop, is a problem, because anyone who can use the computer can read it.
Using the built-in password generator of a site. Some sites suggest a password when you sign up. These suggestions are often short or limited to a small set of characters. If a manager can make a longer password for the same site, use the manager.
Forgetting the master password. If you cannot unlock the manager, you cannot read any saved password. The written backup, described above, is the only way back in for most managers. Test that you can unlock the vault on a second device after you set it up.
Trusting the manager's autofill on the wrong address. If your manager offers to fill a password on a page with a strange address, stop. The page may be a fake that copies the look of the real site. Check the address in the bar before you continue.
Real error messages from a password generator
Password code can fail for simple reasons, and the error messages look alarming. Here is the last line Python prints when the word list is empty. This happens if a list is loaded from a file that has no words in it, and the program still tries to pick one:
IndexError: Cannot choose from an empty sequenceThe fix is to check the list before you pick from it. Load the file, count the words, and stop with a clear message if the count is zero.
Another common error is a value that is too small. Passing zero to secrets.randbelow gives this message:
ValueError: Upper bound must be positive.This happens when the code computes the size of a list as zero and then asks for a random index inside it. The fix is the same as before: check the size of the list first.
Practice
- Your passphrase uses six words from the 7,776-word list. How many bits of entropy does it have, to one decimal place? Use log2(7776) = 12.92 bits per word.
Show the answer
Six words times 12.92 bits is 77.52, which rounds to 77.5 bits. The program in this lesson printed 77.5 bits for the same case.
- Which is stronger: a password of 8 random characters from the 94 printable symbols and letters and digits, or a passphrase of 4 random words from the 7,776-word list? Calculate both.
Show the answer
Eight characters from 94 symbols gives 8 times log2(94), which is about 52.4 bits. Four words from 7,776 gives 4 times 12.92, which is about 51.7 bits. The character password is slightly stronger, but the difference is small. Both are far stronger than a word the person chose.
- Your email, bank and a forum all use the same password. The forum is breached. Explain what an attacker can do, and what you should do first.
Show the answer
The attacker has the forum's stored pair of email and password. They can try the same pair on your email and your bank, because you reused the password. If the email account falls, the attacker can reset the other passwords through it. The first step is to change the email password and the bank password, and to make each new password unique. Start with the email, because password reset messages go there.
- Look at this code. Why does it fail, and what is the fix?
import secrets
words = []
print(secrets.choice(words))Show the answer
The list is empty, so there is nothing to choose from. Python raises IndexError: Cannot choose from an empty sequence. The fix is to fill the list from a word file and check that it has words before the choice is made, for example with if not words: raise SystemExit("word list is empty").
- Your manager offers to fill your bank password into a page whose address is
bank-secure-login.example.net, while your bank's address isbank.example.com. What should you do?
Show the answer
Do not continue. The address is different from the bank's own, so the page is probably a copy made to steal the password. Close the page, and open the bank by typing its address yourself or by using a bookmark you made earlier. A good manager does not fill passwords into pages with different addresses, so the fact that it offered is a warning sign.
Summary
- A password is strong because of how it was made. Random choices with many options beat patterns that feel complicated.
- A passphrase of six random words from a list of 7,776 gives about 77 bits of entropy and is easy to type.
- Use a different password on every site, so that one breach cannot open your other accounts.
- A password manager stores long random passwords in an encrypted vault, unlocked by one master password that you must remember and back up.
- Test that you can unlock your vault on a second device, and check the address before the manager fills in a password.