Examples of Cryptography in Python: 3 Practical Projects You Can Actually Build
Most tutorials start with long definitions. Let’s skip that and jump straight into examples of cryptography in Python: 3 practical examples you can build:
- A Caesar cipher and substitution cipher you write from scratch
- A password locker that uses hashing instead of storing real passwords
- A mini secure messenger that uses modern encryption (the kind real apps use)
These three projects give you both beginner-friendly and real-world examples. You’ll see how the same ideas show up in web logins, banking, and messaging apps.
Example 1: Classic Ciphers in Python – Caesar and Substitution
If you want a science fair project that feels like secret-agent work, this is your warm-up. The first example of cryptography in Python is a simple Caesar cipher, followed by a slightly more advanced substitution cipher.
Caesar Cipher: The “Shift the Alphabet” Trick
The Caesar cipher is one of the oldest examples of encryption. You shift each letter by a fixed number. For example, with a shift of 3:
- A → D
- B → E
- C → F
So HELLO becomes KHOOR.
Here’s a simple Caesar cipher in Python:
import string
ALPHABET = string.ascii_uppercase
def caesar_encrypt(plaintext, shift):
plaintext = plaintext.upper()
ciphertext = ""
for char in plaintext:
if char in ALPHABET:
index = ALPHABET.index(char)
new_index = (index + shift) % 26
ciphertext += ALPHABET[new_index]
else:
ciphertext += char # keep spaces and punctuation
return ciphertext
def caesar_decrypt(ciphertext, shift):
return caesar_encrypt(ciphertext, -shift)
message = "MEET ME AT NOON"
shift = 5
encrypted = caesar_encrypt(message, shift)
decrypted = caesar_decrypt(encrypted, shift)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
This is one of the best examples for beginners because:
- You can explain it with a simple diagram of the alphabet.
- You can demonstrate attacks: try all 26 shifts and break it.
- You can show why modern cryptography had to evolve far beyond this.
Substitution Cipher: Mixing Up the Alphabet
A substitution cipher is another classic example of cryptography in Python. Instead of shifting letters, you map each letter to a completely different one.
For example:
- A → Q
- B → M
- C → Z
- ... and so on
Here’s a basic substitution cipher using a key alphabet:
import string
PLAIN = string.ascii_uppercase
KEY = "QWERTYUIOPASDFGHJKLZXCVBNM" # substitution alphabet
mapping_encrypt = {p: k for p, k in zip(PLAIN, KEY)}
mapping_decrypt = {k: p for p, k in zip(PLAIN, KEY)}
def substitute_encrypt(plaintext):
plaintext = plaintext.upper()
result = ""
for char in plaintext:
if char in mapping_encrypt:
result += mapping_encrypt[char]
else:
result += char
return result
def substitute_decrypt(ciphertext):
ciphertext = ciphertext.upper()
result = ""
for char in ciphertext:
if char in mapping_decrypt:
result += mapping_decrypt[char]
else:
result += char
return result
msg = "TOP SECRET MESSAGE"
enc = substitute_encrypt(msg)
dec = substitute_decrypt(enc)
print("Encrypted:", enc)
print("Decrypted:", dec)
This gives you several real examples to talk about in your project:
- You can compare Caesar vs substitution: which is harder to break?
- You can introduce frequency analysis (counting letters like E, T, A) and show how to attack it.
- You can connect it to historical ciphers used before computers.
For a science fair board, you can print sample messages, show the mappings, and even let visitors encrypt their own name.
Example 2: Password Locker with Hashing – From Plaintext to Protection
Now let’s move from historical to modern. Storing passwords directly is a terrible idea. If a file is stolen, attackers get everything. Modern systems store hashes of passwords instead of the actual passwords.
This second project is one of the best examples of cryptography in Python for 2024–2025 because it matches how real websites protect (or sometimes fail to protect) users.
Hashing: One-Way Math
A hash function takes input and produces a fixed-length output. A few important points you can explain:
- Same input → same output
- Tiny change in input → huge change in output
- You can’t easily reverse the hash to get the original password
Python includes secure hash functions in the hashlib module, like SHA-256, which is commonly used in security and blockchain research. You can read more about modern cryptography and standards through resources like the National Institute of Standards and Technology (NIST).
Here’s a simple password locker that stores hashed passwords:
import hashlib
users = {} # username -> password_hash
def hash_password(password):
# # encode to bytes, then hash, then get hex string
return hashlib.sha256(password.encode("utf-8")).hexdigest()
def register(username, password):
if username in users:
print("Username already exists")
return
users[username] = hash_password(password)
print("User registered")
def login(username, password):
if username not in users:
print("No such user")
return False
return users[username] == hash_password(password)
## Demo
register("alice", "My$tr0ngP@ss")
print("Login correct:", login("alice", "My$tr0ngP@ss"))
print("Login wrong:", login("alice", "wrongpass"))
You can expand this into a full science fair project by:
- Showing how storing raw passwords is dangerous.
- Demonstrating hashes for different passwords.
- Showing how changing even one character changes the hash completely.
Going Further: Salting and Stronger Password Storage
In real systems, just hashing is not enough. Websites use salts (random data added to each password before hashing) and slower, specialized functions like bcrypt or scrypt to make attacks harder.
In Python, the bcrypt library is widely used. Here’s a short example of using it:
import bcrypt
password = "My$tr0ngP@ss".encode("utf-8")
## generate salt and hash
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(password, salt)
print("Salt:", salt)
print("Hashed:", hashed)
## verifying
print("Correct password?", bcrypt.checkpw(password, hashed))
print("Wrong password?", bcrypt.checkpw(b"wrong", hashed))
This gives you another layer of examples of cryptography in Python: 3 practical examples because now you can:
- Compare simple SHA-256 hashing vs
bcrypt. - Talk about why websites in 2024–2025 are expected to use stronger password storage.
- Reference real-world data breaches and how poor password storage made them worse.
For background on password and authentication best practices, you can consult security guidance from organizations like CISA.gov and standards from NIST.
Example 3: Secure Messaging with Modern Encryption (Fernet / AES)
The third project is where your science fair display starts to feel like a modern app. Instead of toy ciphers, you’ll use real cryptographic libraries to encrypt messages the way secure chat apps do.
Python’s cryptography library provides high-level tools that wrap advanced algorithms like AES. One of the best examples for beginners is Fernet, which handles encryption, decryption, and integrity checks for you.
Symmetric Encryption with Fernet
In symmetric encryption, the same key is used to encrypt and decrypt messages.
Here’s a minimal secure messenger example of cryptography in Python using Fernet:
from cryptography.fernet import Fernet
## Generate a key (this should be shared secretly between users)
key = Fernet.generate_key()
fernet = Fernet(key)
message = "Meet at the library at 3 PM".encode("utf-8")
## Encrypt
token = fernet.encrypt(message)
print("Encrypted:", token)
## Decrypt
decrypted = fernet.decrypt(token)
print("Decrypted:", decrypted.decode("utf-8"))
This one short script gives you several real examples to explain:
- How a random key is generated.
- How the same key can unlock the message.
- What happens if someone changes even one byte of the encrypted token (Fernet will refuse to decrypt it).
You can turn this into a mini chat simulation:
- “User A” runs the program and encrypts a message.
- You copy the encrypted token as if it’s traveling over the internet.
- “User B” pastes it into a decryption script using the same key.
Public-Key Twist: Signing Messages
Modern systems often use public-key cryptography: one key to sign or decrypt, another key to verify or encrypt. While building a full public-key system is more advanced, you can still show a simple signing example of public-key cryptography in Python.
Using the cryptography library again, you can generate a key pair and sign messages. This is similar to how software updates, secure email, and some messaging apps verify that a message really came from the right person.
For a high-school or early college project, you don’t need to write all the math. Instead, you can:
- Show that a message signed with one key can be verified with the matching public key.
- Explain that this prevents attackers from pretending to be someone else.
If you want to connect this to real-world standards, you can mention that modern protocols like TLS (used in HTTPS) use a combination of public-key and symmetric cryptography. The Mozilla Developer Network has student-friendly explanations of HTTPS and TLS.
More Real Examples of Cryptography in Python You Can Mention
For a strong science fair or class project, it helps to show that your three main projects sit inside a bigger world. Here are more examples of cryptography in Python that you can describe, even if you don’t fully implement all of them:
- File encryption tool: Use Fernet to encrypt and decrypt files on disk (like a mini version of a secure USB drive).
- Secure note-taking app: Encrypt notes before saving them, then decrypt with a master password.
- Checksum and integrity checker: Use
hashlibto compute hashes of files, then show how changing even one byte changes the hash. - API key protector: Encrypt API keys or tokens before storing them in configuration files.
- Two-factor demo: Simulate one-time codes by generating random numbers and hashing timestamps.
- Simple blockchain demo: Use hashes to link blocks of data together and show how changing one block breaks the chain.
These extra ideas help you answer the classic science fair question: “So where is this used in real life?” You can tie them to things people already know, like password managers, cloud backups, and secure messaging.
How to Turn These 3 Practical Examples into a Strong Science Fair Project
You now have several examples of cryptography in Python: 3 practical examples at the core, plus extra ideas around them. Here’s how to turn them into a clear, impressive project without drowning your audience in formulas.
Build a Clear Story
Organize your project like a timeline:
- Start with classic ciphers (Caesar and substitution) to show where we began.
- Move to password hashing to explain how websites protect users today.
- Finish with secure messaging to connect to apps people use every day.
At each step, show:
- A short code snippet
- A sample input and output
- A simple explanation of “what changed” and “why it’s safer”
Show Attacks and Defenses
Science fairs love cause-and-effect. Don’t just show that encryption works; show how weak methods fail.
You can:
- Break your own Caesar cipher by trying all 26 shifts.
- Show how storing plain passwords lets you read everyone’s password.
- Compare fast hashing vs slower
bcryptand explain why slower can be safer.
You can also mention real-world incidents where poor cryptography caused problems. While health-focused sites like Mayo Clinic or NIH are more about medicine than encryption, they’re good examples of organizations that must protect sensitive data using modern security practices.
Explain Without Jargon
Your goal is not to sound like a textbook. Your goal is for a middle-school student, a parent, and a teacher to all walk away thinking, “I actually get this.”
Use comparisons:
- Encryption key → a special key that locks and unlocks a box.
- Hash → a fingerprint of data that changes if the data changes.
- Public key → a lock everyone can have.
- Private key → the only key that can open that lock.
Then back up those stories with your examples of cryptography in Python: show the code, the output, and a one-sentence explanation.
FAQ: Common Questions About Python Cryptography Projects
What are some simple examples of cryptography in Python for beginners?
Some great starting points include a Caesar cipher, a substitution cipher, a basic password hasher using hashlib, and a Fernet-based message encryptor from the cryptography library. These examples include both historical and modern methods and are easy to explain in a science fair setting.
Is it safe to use my own encryption instead of libraries?
For learning and science fairs, writing your own cipher is fine. For real security, it’s a bad idea. Modern cryptography is based on well-studied algorithms and standards, tested by experts and organizations like NIST. In real apps, you should rely on proven libraries instead of inventing new schemes.
Which Python library should I use for real examples of encryption?
For realistic, modern examples of cryptography in Python, the cryptography library is a strong choice. Its Fernet module gives you authenticated encryption with a simple interface, and it wraps industry-standard algorithms like AES under the hood.
Can I use these 3 practical examples in a science fair project?
Yes. These examples of cryptography in Python: 3 practical examples were chosen with science fairs and classroom demos in mind. They are visual, easy to explain, and directly connected to real technologies like secure logins and messaging apps.
Do I need an internet connection to run these Python cryptography examples?
You only need internet access to install libraries like cryptography or bcrypt the first time. After that, all of your examples of cryptography in Python run locally on your computer, which is perfect for school environments where Wi‑Fi may not be reliable.
By combining classic ciphers, password hashing, and secure messaging, you get a full story: where cryptography started, how it protects us online today, and how Python lets you experiment with it yourself. That’s exactly the kind of project that stands out on a science fair table.
Related Topics
Examples of Game Development with Scratch: 3 Fun Examples for Students
The Best Examples of 3 Simple Database Examples with SQL for Beginners
Examples of Cryptography in Python: 3 Practical Projects You Can Actually Build
Best examples of Unity virtual environment simulation examples for science fairs
Examples of Weather App Using APIs: 3 Practical Projects You Can Actually Build
Top real examples of e-commerce website development examples for 2025
Explore More Computer Science Projects
Discover more examples and insights in this category.
View All Computer Science Projects