Please enable JavaScript to use CodeHS

Cybersecurity Tutorial

How Strong is Your Password?

Hackers do not guess passwords by hand. They use programs that try millions of guesses every second. Learn the three attacks they use, then build a password that would take millions of years to crack.

cybersecurity ~15 min No experience needed
The Big Idea

Your password guards everything

Think about what sits behind your passwords: your messages, your photos, your grades, maybe even your family's money. A password is the lock on all of it. If the lock is weak, everything behind it is up for grabs.

Here is the part most people get wrong: hackers almost never sit there typing guesses one at a time. They write programs that guess for them, and those programs can try millions or even billions of passwords every second. Your password does not need to beat a person. It needs to beat a computer.

In this tutorial you will learn the three main ways attackers crack passwords, see the math that makes some passwords basically uncrackable, and test password ideas in a live strength checker.

Good news: once you understand how the attacks work, making an uncrackable password is easy. The defenders have the math on their side.


Attack 1 of 3

Brute force: try everything

A brute force attack is the simplest attack there is. The program tries every possible combination, one after another, until something works: aaaa, aaab, aaac, and so on. No cleverness, just speed.

How fast is that in practice? Think about a 4-digit PIN, like the code on a phone or a locker. There are only 10,000 possibilities (0000 through 9999). Here is a brute force attack on a PIN, written in Python. Press Run:

brute_force.py
# The secret 4-digit code we are trying to crack
secret = "0412"

guesses = 0
for number in range(10000):
    guesses = guesses + 1
    # Turn 412 into "0412" so it looks like a PIN
    guess = str(number).zfill(4)
    if guess == secret:
        print("Cracked it! The code was " + guess)
        print("It took " + str(guesses) + " guesses.")
        break
Console
Cracked it! The code was 0412
It took 413 guesses.

413 guesses sounds like a lot for a person. For a computer, it is nothing. Even trying all 10,000 codes takes a modern computer less than a millisecond. That is why a short password, even a totally random one, is never safe on its own.

Try this thought experiment: in the original version of this tutorial, you play a game guessing a secret number from 0 to 99, and the game tells you "higher" or "lower" after each guess. With those hints you can always win in 7 guesses or fewer, by cutting the range in half each time. Real login systems never give hints like that, and now you know why!

Attack 2 of 3

Dictionary attacks: guess the likely words first

Trying everything takes a long time when passwords get longer. So attackers get smarter. A dictionary attack skips the random combinations and tries a list of likely passwords first: real words, names, sports teams, and every password that has ever leaked onto the internet.

Millions of people use the same handful of passwords. These show up in every attacker's list, so they get cracked in the first second of any attack:

123456 password qwerty letmein iloveyou dragon monkey abc123 football princess

A dictionary attack is only a few lines of code. Watch it tear through a password that looks fine but is a common word:

dictionary_attack.py
# Real attack lists have millions of entries.
# This tiny one is enough to crack a LOT of accounts.
word_list = ["123456", "password", "qwerty", "letmein",
             "iloveyou", "dragon", "monkey", "abc123",
             "football", "princess"]

secret = "dragon"

attempts = 0
for word in word_list:
    attempts = attempts + 1
    print("Trying: " + word)
    if word == secret:
        print("Cracked it in " + str(attempts) + " tries!")
        break
Console
Trying: 123456
Trying: password
Trying: qwerty
Trying: letmein
Trying: iloveyou
Trying: dragon
Cracked it in 6 tries!

The rule: if your password is a word you could find in a dictionary, a name, or anything from a "most common passwords" list, a dictionary attack cracks it instantly. Length does not save you here: tyrannosaurus is 13 letters long and still on the list.

Attack 3 of 3

Pattern attacks: computers know your tricks

Maybe you are thinking: "Fine, I will take a word and disguise it. dragon becomes Dr@g0n1!. Problem solved?" Not quite. Attackers know every trick that humans use, and they build those tricks right into their guessing programs. This is a pattern attack (sometimes called an algorithm attack).

A pattern attack takes each dictionary word and automatically tries the popular variations: swap a for @, swap o for 0, capitalize the first letter, stick a 1 or ! on the end. Each variation is just one more guess, and the computer makes millions of guesses per second.

Brute force

Tries every possible combination in order. Unbeatable against short passwords, hopeless against long ones.

beats short passwords

Dictionary

Tries a huge list of real words, names, and leaked passwords first. Cracks any password that is "a thing".

beats common words

Pattern

Takes dictionary words and auto-applies human tricks: @ for a, 0 for o, capital first letter, ! at the end.

beats disguised words

Personal info counts too. Attackers also feed in things they can learn about you: your name, your birthday, your pet, your team. Bella2013! feels personal and unguessable to you. To a pattern attack, it is guess number twelve.


The Secret Weapon

The math: why length wins

So how do you beat a program that guesses a billion passwords per second? With multiplication.

Every character in your password is picked from a set of possible characters. Lowercase letters give you 26 choices. Add uppercase and you have 52. Add digits: 62. Add symbols like ! @ # $: about 94. The total number of possible passwords is:

combinations = choices per character, multiplied by itself once for every character  (in math terms: choiceslength)

That little exponent is your superpower. Every single character you add multiplies the attacker's work by 26, 62, or 94. Watch what happens:

password_math.py
GUESSES_PER_SECOND = 1000000000  # one billion

# 8 characters, lowercase letters only (26 choices)
combos = 26 ** 8
print("Lowercase, 8 chars:", combos, "combinations")
print("Cracked in", combos / GUESSES_PER_SECOND, "seconds")

# 12 characters, using all 94 character types
combos = 94 ** 12
years = combos / GUESSES_PER_SECOND / 60 / 60 / 24 / 365
print("Everything, 12 chars:", combos, "combinations")
print("Cracked in about", round(years), "years")
Console
Lowercase, 8 chars: 208827064576 combinations
Cracked in 208.827064576 seconds
Everything, 12 chars: 475920314814253376475136 combinations
Cracked in about 15091334 years

Read that again. An 8-character lowercase password falls in under 4 minutes. A 12-character password using every character type holds out for about 15 million years. Same attacker, same speed. The only difference is length and variety.


Interactive

Test a password

Time to try it yourself. Type a made-up password below and watch the math update live. The checker counts your character choices, computes the combinations, and estimates how long a brute force attack would take at one billion guesses per second.

password_lab - strength analyzer
Never type a real password here or on any website like this. Make one up just for testing. Rule one of password security: your real password goes only into the real login page.
Waiting... Type something to analyze it.
Choices per character
–
Possible combinations
–
Time to crack (1 billion guesses/sec)
–
At least 12 characters
Has an uppercase letter
Has a number
Has a symbol (!, @, #, ...)
Not on the common-passwords list

Keep it honest: this estimate is for a pure brute force attack. If your password is a dictionary word with predictable swaps, real attackers will crack it far faster than the math above suggests. The checklist catches the obvious cases, but "random and long" is the only combination that wins every time.


Cheat Sheet

Do this, not that

Everything in this tutorial boils down to a few habits. Screenshot this part:

Strong password habits
  • Make it long: 12 characters or more. Length is the single biggest factor.
  • Mix uppercase, lowercase, numbers, and symbols.
  • Try a passphrase: several random words like plaid-otter-thunder-42 are long and easy to remember.
  • Use a different password for every account.
  • Turn on two-factor authentication where you can.
Weak password habits
  • A single dictionary word, even a long one.
  • Personal info: names, birthdays, pets, teams.
  • "Clever" swaps like P@ssw0rd!. Pattern attacks try those automatically.
  • Reusing one password everywhere. One leak unlocks everything.
  • Sharing your password, even with friends.

Code

A password strength checker

Good passwords are long and include different kinds of characters. This Python function scores a password using simple rules.


Check Yourself

Quick quiz

1. What does a brute force attack do?

2. Which password would a dictionary attack crack fastest?

3. You add one more character to a password that uses all 94 character types. What happens to the number of possible combinations?

Keep learning cybersecurity

Strong passwords are just the first line of defense. Next, learn to spot the scam emails that steal passwords without cracking anything, or explore the ciphers that keep your data secret in transit.