DEV Community

Ethan Callahan
Ethan Callahan

Posted on

How to Build a Simple Encryption and Decryption Tool

Encryption and decryption are important concepts in computer science, cybersecurity, and software development. Almost every modern application handles some type of sensitive information, such as passwords, personal details, messages, payment information, or confidential documents. Encryption helps protect this information by transforming readable data into a protected format.

For students, building a simple encryption and decryption tool is an excellent programming project because it combines basic programming concepts with an introduction to cybersecurity. It can help learners understand strings, characters, mathematical operations, functions, user input, file handling, and basic cryptographic ideas.

In this assignment, we will understand how to build a simple encryption and decryption tool using Python. The project will use a basic substitution approach that is suitable for learning purposes. It should not be considered a replacement for modern encryption libraries used in real applications.

What Is Encryption

Encryption is the process of converting readable information into an encoded form that is difficult for an unauthorized person to understand.

The original readable information is called plaintext. After encryption, the resulting information is called ciphertext.

For example, imagine that a user enters the message

HELLO
Enter fullscreen mode Exit fullscreen mode

A simple encryption algorithm can transform it into another representation such as

KHOOR
Enter fullscreen mode Exit fullscreen mode

The exact transformation depends on the encryption method being used.

The purpose of encryption is to protect information from unauthorized access.

What Is Decryption

Decryption is the reverse process of encryption.

It converts ciphertext back into the original plaintext using an appropriate method or key.

For example

Plaintext → Encryption → Ciphertext
Ciphertext → Decryption → Plaintext
Enter fullscreen mode Exit fullscreen mode

If the original message was

HELLO
Enter fullscreen mode Exit fullscreen mode

and the encryption process produced

KHOOR
Enter fullscreen mode Exit fullscreen mode

the decryption process should convert it back to

HELLO
Enter fullscreen mode Exit fullscreen mode

A successful encryption and decryption tool therefore needs to implement both processes correctly.

Encryption and Decryption in Cybersecurity

Encryption plays an important role in cybersecurity because data can travel through networks and may be stored on computers, servers, and mobile devices.

Some common applications include

  1. Protecting files
  2. Securing online communication
  3. Protecting sensitive databases
  4. Securing financial transactions
  5. Protecting personal information
  6. Supporting secure authentication systems
  7. Protecting stored backups

Modern applications use advanced cryptographic algorithms and carefully designed key management systems. Examples include AES and RSA.

For a beginner programming assignment, however, a simple algorithm is useful for understanding the fundamental concept.

Project Objective

The main objective of this project is to create a small Python program that can encrypt and decrypt text.

The tool should allow the user to

  1. Enter a message
  2. Enter a numerical key
  3. Encrypt the message
  4. Decrypt the encrypted message
  5. Display the result

The project also demonstrates how the same key can be used to reverse a transformation.

Understanding the Basic Encryption Method

For this project, we can use a simple Caesar style transformation.

The basic idea is straightforward.

Every alphabetic character is shifted by a particular number of positions.

For example, suppose the key is 3.

The transformation becomes

A → D
B → E
C → F
D → G
Enter fullscreen mode Exit fullscreen mode

Continuing the same pattern

X → A
Y → B
Z → C
Enter fullscreen mode Exit fullscreen mode

The wrapping behavior is important because the alphabet contains only 26 letters.

For decryption, the process is reversed.

D → A
E → B
F → C
Enter fullscreen mode Exit fullscreen mode

This provides a simple example of how an encryption key can control a transformation.

Important Note About Security

A Caesar style cipher is useful for education but is not secure for protecting real confidential information.

It has a very small key space and can be broken easily. Modern encryption systems use mathematically sophisticated algorithms, secure keys, carefully designed modes, authentication, and other protections.

Therefore, this project should be presented as an educational demonstration of encryption concepts rather than a production security solution.

Requirements for the Project

The project does not require expensive hardware or complicated software.

Hardware Requirements

A basic computer or laptop is sufficient.

Recommended hardware includes

  1. A computer with at least 4 GB RAM
  2. A keyboard
  3. A display
  4. Basic storage space

Software Requirements

You can use

  1. Python
  2. Visual Studio Code
  3. PyCharm
  4. IDLE
  5. Any other Python compatible editor

Python is particularly suitable because its syntax is simple and easy for beginners to understand.

Designing the Tool

Before writing code, it is useful to divide the project into smaller components.

The program can contain the following functions

Start Program
       ↓
Display Menu
       ↓
Choose Encryption or Decryption
       ↓
Enter Message
       ↓
Enter Key
       ↓
Process Message
       ↓
Display Result
       ↓
Exit
Enter fullscreen mode Exit fullscreen mode

This approach makes the program easier to understand and debug.

Python Implementation

The following example demonstrates a simple educational encryption and decryption tool.

def encrypt(text, key):
    result = ""

    for char in text:
        if char.isupper():
            result += chr((ord(char) - ord('A') + key) % 26 + ord('A'))
        elif char.islower():
            result += chr((ord(char) - ord('a') + key) % 26 + ord('a'))
        else:
            result += char

    return result


def decrypt(text, key):
    return encrypt(text, -key)


message = input("Enter your message: ")
key = int(input("Enter encryption key: "))

encrypted = encrypt(message, key)
decrypted = decrypt(encrypted, key)

print("Encrypted message:", encrypted)
print("Decrypted message:", decrypted)
Enter fullscreen mode Exit fullscreen mode

This program demonstrates the basic relationship between encryption and decryption.

Understanding the Code

The program begins by defining an encryption function.

def encrypt(text, key):
Enter fullscreen mode Exit fullscreen mode

The function receives two values.

The first is the message and the second is the encryption key.

A result variable is then created.

result = ""
Enter fullscreen mode Exit fullscreen mode

This variable stores the transformed characters.

The program then examines every character in the message.

for char in text:
Enter fullscreen mode Exit fullscreen mode

The program checks whether the character is uppercase.

if char.isupper():
Enter fullscreen mode Exit fullscreen mode

If it is an uppercase letter, the program applies the shift.

The same approach is then applied to lowercase letters.

elif char.islower():
Enter fullscreen mode Exit fullscreen mode

Characters that are not alphabetic are preserved.

else:
    result += char
Enter fullscreen mode Exit fullscreen mode

This means spaces, numbers, and punctuation marks remain unchanged in this simple implementation.

Understanding ord and chr

Two Python functions are especially important in this program.

They are ord() and chr().

The ord() function converts a character into its numerical Unicode representation.

For example

ord('A')
Enter fullscreen mode Exit fullscreen mode

returns a numerical value representing the character.

The chr() function performs the opposite operation.

It converts a numerical value into a character.

These functions allow the program to perform mathematical operations on letters.

Why Modulo 26 Is Used

The expression contains

% 26
Enter fullscreen mode Exit fullscreen mode

The modulo operator ensures that the result remains within the alphabet.

There are 26 letters in the English alphabet.

Suppose the current character is Z and the key is 3.

Without modulo handling, the calculation could move beyond Z.

Modulo 26 allows the calculation to wrap around to the beginning of the alphabet.

Therefore

Z → C
Enter fullscreen mode Exit fullscreen mode

when the key is 3.

This is one of the most important concepts in the implementation.

Understanding the Decryption Function

The decryption function is extremely simple.

def decrypt(text, key):
    return encrypt(text, -key)
Enter fullscreen mode Exit fullscreen mode

Instead of creating a completely separate transformation algorithm, the program uses the encryption function with a negative key.

If encryption shifts characters forward by a certain amount, decryption shifts them backward by the same amount.

For example

Encryption key = 3
Decryption key = -3
Enter fullscreen mode Exit fullscreen mode

This demonstrates an important programming principle.

A well designed function can sometimes be reused instead of duplicating similar code.

Example of Program Execution

Suppose the user enters

Enter your message: Hello World
Enter encryption key: 3
Enter fullscreen mode Exit fullscreen mode

The program may produce

Encrypted message: Khoor Zruog
Decrypted message: Hello World
Enter fullscreen mode Exit fullscreen mode

The spaces remain unchanged.

The uppercase H remains uppercase and lowercase characters remain lowercase.

Creating a Menu Based Tool

The project can be improved by adding a menu.

A menu based program is more interactive and looks more like a real application.

def encrypt(text, key):
    result = ""

    for char in text:
        if char.isupper():
            result += chr((ord(char) - ord('A') + key) % 26 + ord('A'))
        elif char.islower():
            result += chr((ord(char) - ord('a') + key) % 26 + ord('a'))
        else:
            result += char

    return result


def decrypt(text, key):
    return encrypt(text, -key)


while True:
    print("\nEncryption and Decryption Tool")
    print("1. Encrypt")
    print("2. Decrypt")
    print("3. Exit")

    choice = input("Enter your choice: ")

    if choice == "1":
        message = input("Enter message: ")
        key = int(input("Enter key: "))
        print("Encrypted message:", encrypt(message, key))

    elif choice == "2":
        message = input("Enter encrypted message: ")
        key = int(input("Enter key: "))
        print("Decrypted message:", decrypt(message, key))

    elif choice == "3":
        print("Program closed.")
        break

    else:
        print("Invalid choice.")
Enter fullscreen mode Exit fullscreen mode

This version allows the user to decide whether they want to encrypt or decrypt information.

Improving User Input Validation

A good project should handle incorrect input.

For example, the user might enter letters instead of a number when the program expects a key.

The program can use exception handling.

try:
    key = int(input("Enter key: "))
except ValueError:
    print("Please enter a valid number.")
Enter fullscreen mode Exit fullscreen mode

This prevents the program from terminating unexpectedly.

Input validation is important because real applications should not assume that users always provide correct information.

Handling Large Keys

A user could enter a key larger than 26.

For example

key = 29
Enter fullscreen mode Exit fullscreen mode

The mathematical operation can still work because the modulo operation effectively reduces the shift.

A key of 29 has the same rotational effect as a key of 3 for this particular educational cipher.

The program can also normalize the key.

key = key % 26
Enter fullscreen mode Exit fullscreen mode

This keeps the key within the alphabet range.

Testing the Encryption Tool

Testing is an important part of any programming assignment.

The program should be tested with different types of input.

Test One

Input

Hello
Enter fullscreen mode Exit fullscreen mode

Key

3
Enter fullscreen mode Exit fullscreen mode

Expected encrypted output

Khoor
Enter fullscreen mode Exit fullscreen mode

Test Two

Input

ABC
Enter fullscreen mode Exit fullscreen mode

Key

1
Enter fullscreen mode Exit fullscreen mode

Expected output

BCD
Enter fullscreen mode Exit fullscreen mode

Test Three

Input

XYZ
Enter fullscreen mode Exit fullscreen mode

Key

3
Enter fullscreen mode Exit fullscreen mode

Expected output

ABC
Enter fullscreen mode Exit fullscreen mode

Test Four

Input

Hello World 123
Enter fullscreen mode Exit fullscreen mode

Key

5
Enter fullscreen mode Exit fullscreen mode

The letters should be transformed while spaces and numbers should remain unchanged.

Test Five

The encrypted message should be decrypted using the same key.

The final result should match the original message.

Common Mistakes Students Make

Several mistakes can occur while creating this project.

Forgetting Alphabet Wrapping

A common mistake is failing to handle letters near the end of the alphabet.

For example, Z should wrap back to the beginning when the shift continues.

Changing Spaces

A beginner may accidentally transform spaces along with letters.

The program should identify alphabetic characters separately.

Losing Letter Case

Another common issue is converting every letter to uppercase or lowercase.

A better implementation preserves the original case.

Incorrect Decryption Key

If encryption uses a positive shift, decryption must reverse that shift.

Using the same direction for both operations can produce incorrect results.

Not Validating Input

Invalid input can cause errors.

Using proper validation and exception handling makes the program more reliable.

Treating a Simple Cipher as Secure

This is perhaps the most important conceptual mistake.

A Caesar style cipher should not be used to protect real passwords, financial information, private documents, or other sensitive data.

How Modern Encryption Is Different

Real encryption is much more advanced than the simple transformation demonstrated in this project.

Modern cryptographic systems are designed to resist practical attacks.

For example, AES is a widely used symmetric encryption algorithm.

In symmetric encryption, the same secret key is generally used for encryption and decryption.

Public key cryptography works differently.

Systems such as RSA use a public key and a private key for different cryptographic operations.

Modern secure applications may also use authenticated encryption, secure random number generation, key derivation, digital signatures, certificates, and secure key storage.

Therefore, students should understand that this project introduces the basic idea of transformation and reversibility rather than implementing modern cryptographic security.

Using Python Libraries for Real Applications

When developing a real application, developers should normally rely on established cryptographic libraries rather than designing their own encryption algorithm.

Creating cryptographic algorithms from scratch can introduce serious security weaknesses.

For educational projects, implementing a simple cipher helps explain the concept.

For production software, professionally reviewed cryptographic libraries should be preferred.

Adding File Encryption as a Future Feature

A useful extension of this project is file processing.

Instead of asking the user to enter a short message, the application could read text from a file and process the contents.

A possible workflow is

Select File
     ↓
Read File
     ↓
Process Data
     ↓
Save Result
Enter fullscreen mode Exit fullscreen mode

For a classroom assignment, this feature can demonstrate file handling in Python.

However, file encryption intended for real security should use an established cryptographic library and a secure encryption scheme.

Creating a Graphical Interface

Another improvement is adding a graphical user interface.

A Python library such as Tkinter can be used to create

  1. Text input boxes
  2. Buttons
  3. Key fields
  4. Encryption output areas
  5. Decryption controls
  6. Error messages

The interface could look like

-------------------------------------
       ENCRYPTION TOOL
-------------------------------------

Enter Message
[____________________________]

Enter Key
[________]

[ Encrypt ]     [ Decrypt ]

Result
[____________________________]

-------------------------------------
Enter fullscreen mode Exit fullscreen mode

This can make the project more attractive during a college demonstration.

Possible Project Features

A more advanced version of the assignment can include

  1. Encryption and decryption
  2. Menu based navigation
  3. Input validation
  4. File processing
  5. Graphical interface
  6. Copy result button
  7. Clear button
  8. Error handling
  9. Activity logging
  10. Secure cryptographic libraries for advanced versions

Students can implement these features progressively.

Project Development Process

A good development process can make the assignment easier.

Step One

Define the purpose of the tool.

Step Two

Select an educational encryption method.

Step Three

Create the encryption function.

Step Four

Create the decryption function.

Step Five

Add user input.

Step Six

Add validation.

Step Seven

Test different messages and keys.

Step Eight

Improve the interface.

Step Nine

Document the project.

Step Ten

Prepare screenshots and test results for the assignment report.

Suggested Assignment Report Structure

Students can organize their report in the following format.

Introduction

Explain encryption, decryption, and the purpose of the project.

Objectives

List what the tool is designed to accomplish.

Technologies Used

Mention Python and the development environment.

Methodology

Explain how the encryption and decryption process works.

Algorithm

Describe the character shifting process.

Implementation

Include important sections of the Python code.

Testing

Provide different inputs and expected outputs.

Results

Explain whether the tool successfully encrypted and decrypted messages.

Limitations

Explain why the simple cipher should not be considered secure.

Future Scope

Discuss features such as graphical interfaces, file processing, and modern cryptographic libraries.

Conclusion

Summarize the concepts learned from the project.

How This Project Helps Students

This project is useful because it connects programming with cybersecurity.

Students can practice

  1. Python functions
  2. Loops
  3. Conditional statements
  4. Strings
  5. Character encoding
  6. Mathematical operations
  7. Exception handling
  8. User input
  9. Testing
  10. Software documentation

It also provides a basic introduction to cryptographic thinking.

For students who need additional help with programming assignments, platforms such as Assignment Dude can also help with understanding project requirements, organizing reports, and improving assignment presentation.

Advantages of the Project

A simple encryption and decryption tool has several educational advantages.

Easy to Understand

The algorithm is simple enough for beginners.

Practical

It demonstrates a concept used in real cybersecurity systems.

Good Programming Practice

The project uses functions, loops, conditions, and input handling.

Easy to Extend

Students can add a graphical interface or file processing.

Useful for Demonstration

The tool can be demonstrated easily during a project presentation or viva.

Limitations

The project also has important limitations.

Weak Security

The simple cipher can be broken easily.

Limited Alphabet

The implementation focuses mainly on alphabetic characters.

No Secure Key Management

The user manually provides the key.

No Authentication

The program does not verify whether encrypted data has been modified.

Not Suitable for Sensitive Data

It should not be used for real confidential information.

Understanding these limitations is an important part of a good cybersecurity assignment.

Future Scope

The project can be expanded considerably.

Future versions could explore

  1. AES based encryption
  2. Secure key generation
  3. Password based key derivation
  4. File encryption
  5. Graphical user interfaces
  6. Secure key storage
  7. Authentication
  8. Digital signatures
  9. Secure network communication
  10. Encrypted database storage

These improvements can transform a basic classroom project into a more comprehensive cybersecurity learning project.

Conclusion

Building a simple encryption and decryption tool is an effective way for students to understand the relationship between programming and cybersecurity. The project demonstrates how readable information can be transformed using a key and how the same process can be reversed during decryption.

Using Python, students can create the tool with relatively little code while learning important concepts such as functions, loops, strings, character values, modulo arithmetic, input validation, and testing.

The Caesar style method used in this assignment is intentionally simple and should only be considered an educational example. Real applications require modern cryptographic algorithms and professionally reviewed security libraries.

A well documented project should explain not only how the program works but also its limitations and security implications. This makes the assignment more academically valuable and demonstrates that the student understands both the programming implementation and the cybersecurity concepts behind it.

Frequently Asked Questions

What is an encryption and decryption tool?

An encryption and decryption tool is a program that converts readable information into an encoded form and can later convert that encoded information back into readable information.

Which programming language is best for this project?

Python is an excellent choice for beginners because its syntax is simple and it provides useful features for handling strings, files, and user input.

Is a Caesar cipher secure?

No. A Caesar cipher is mainly useful for learning basic encryption concepts. It is not appropriate for protecting real confidential information.

What is the difference between plaintext and ciphertext?

Plaintext is the original readable information. Ciphertext is the transformed information produced after encryption.

What is an encryption key?

An encryption key is information used by an encryption algorithm to control how data is transformed. In this simple project, the key represents the number of positions by which letters are shifted.

Why is modulo 26 used?

The English alphabet contains 26 letters. Modulo 26 allows the encryption process to wrap around when a transformation moves beyond the final letter.

Can this project encrypt files?

Yes, a future version can be designed to process files. However, secure file encryption should use established cryptographic libraries rather than a basic Caesar cipher.

Can I create a graphical version?

Yes. Python libraries such as Tkinter can be used to create a graphical interface containing text fields, buttons, and result areas.

Can this project be used for real passwords?

No. The simple cipher demonstrated in this assignment is not secure enough for passwords or sensitive information. Real applications should use appropriate password hashing and modern cryptographic techniques.

What can students learn from this project?

Students can learn Python functions, loops, conditional statements, string processing, character encoding, mathematical operations, input validation, testing, and fundamental cybersecurity concepts.

Top comments (0)