AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Title: CRYPTOGRAPHY USING HASH FUNCTIONS
Problem statement:
In today’s world, data privacy and security are of utmost importance in digital
communications. Protecting sensitive information such as files, images, and text
from unauthorized access or tampering is a major challenge. Cryptography,
particularly hash functions, plays a crucial role in ensuring data security, integrity,
and authenticity. Without proper encryption techniques, sensitive data can be
vulnerable to cyberattacks and data breaches.
Project description:
Overview:
This project, "Cryptography Using Hash Functions," aims to explore the role of
hash functions in cryptography, particularly focusing on their application in
encrypting and decrypting data. Hash functions ensure that even a slight change in
the input results in a vastly different hash, providing data integrity and security.
Key Features:
1. Data Security: Using cryptographic hash functions to secure data such as files,
images, and text.
2. Hashing Algorithms: Implementing popular hash functions such as MD5,
SHA-1, and SHA-256.
3. Encryption & Decryption: Demonstrating how hash functions can be used for
file encryption and decryption.
4. Data Integrity: Ensuring that the data has not been tampered with, using
collision resistance, preimage resistance, and the avalanche effect.
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
• Programming Language: Python
• GUI Library: Tkinter
• Data Structure: Hash Functions are used to encrypt and decrypt data,
allowing efficient data security and integrity, also maintaining data privacy.
Target Audience: This project is useful for organizations and individuals looking
to safeguard sensitive digital information. It provides an understanding of how
cryptographic techniques can secure data in real-world applications.
Implementation details:
Hash Functions:
This project implements various hash functions, including MD5, SHA-1, and
SHA-256. Each function takes an input (message) and returns a unique, fixed-size
digest. The project demonstrates the importance of collision resistance, preimage
resistance, and the avalanche effect in maintaining data security.
Code:
from [Link] import Fernet
import hashlib
import os
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox, Toplevel
class FileEncryptor:
def __init__(self, key=None):
if key is None:
key = Fernet.generate_key()
[Link] = key
self.cipher_suite = Fernet([Link])
def encrypt_file(self, file_path):
with open(file_path, 'rb') as file:
file_data = [Link]()
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
encrypted_data = self.cipher_suite.encrypt(file_data)
encrypted_file_path = file_path + '.enc'
with open(encrypted_file_path, 'wb') as encrypted_file:
encrypted_file.write(encrypted_data)
return encrypted_file_path
def decrypt_file(self, encrypted_file_path, output_file_path):
with open(encrypted_file_path, 'rb') as encrypted_file:
encrypted_data = encrypted_file.read()
decrypted_data = self.cipher_suite.decrypt(encrypted_data)
with open(output_file_path, 'wb') as decrypted_file:
decrypted_file.write(decrypted_data)
def encrypt_text(self, text):
encrypted_data = self.cipher_suite.encrypt([Link]())
return encrypted_data
def decrypt_text(self, encrypted_text):
decrypted_data = self.cipher_suite.decrypt(encrypted_text)
return decrypted_data.decode()
def save_key(self, key_file_path):
with open(key_file_path, 'wb') as key_file:
key_file.write([Link])
@staticmethod
def load_key(key_file_path):
with open(key_file_path, 'rb') as key_file:
return key_file.read()
@staticmethod
def hash_file(file_path):
hasher = hashlib.sha256()
with open(file_path, 'rb') as file:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
buf = [Link]()
[Link](buf)
return [Link]()
# Function to handle file upload and encryption/decryption after notification
def upload_file():
file_path = [Link]()
if file_path:
display_notification(file_path)
def input_text():
text = [Link]("Input Text", "Enter text to encrypt or decrypt:")
if text:
display_notification(None, text)
# Function to display the notification after uploading file/text
def display_notification(file_path=None, text=None):
# Create a top-level window for the notification
notif_window = Toplevel(root)
notif_window.title("Cryptography using Hash Functions")
notif_window.geometry("350x200")
# Display message and buttons to either encrypt or decrypt
message = [Link](notif_window, text="File/Text Uploaded Successfully",
fg="black", font=("Arial", 12))
[Link](pady=10)
if file_path:
encrypt_button = [Link](notif_window, text="Encrypt File", font=("Arial",
10), bg="#4CAF50", fg="white",
command=lambda: handle_encryption(file_path,
notif_window, 'file'))
encrypt_button.pack(pady=5)
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
decrypt_button = [Link](notif_window, text="Decrypt File", font=("Arial",
10), bg="#F44336", fg="white",
command=lambda: handle_decryption(file_path,
notif_window, 'file'))
decrypt_button.pack(pady=5)
elif text:
encrypt_button = [Link](notif_window, text="Encrypt Text",
font=("Arial", 10), bg="#4CAF50", fg="white",
command=lambda: handle_encryption(text, notif_window,
'text'))
encrypt_button.pack(pady=5)
decrypt_button = [Link](notif_window, text="Decrypt Text",
font=("Arial", 10), bg="#F44336", fg="white",
command=lambda: handle_decryption(text, notif_window,
'text'))
decrypt_button.pack(pady=5)
notif_window.mainloop()
# Handle encryption for files and text
def handle_encryption(data, notif_window, data_type):
if data_type == 'file':
encrypted_file_path = encryptor.encrypt_file(data)
[Link]("File Encrypted", f'Encrypted file saved to:
{encrypted_file_path}')
elif data_type == 'text':
encrypted_text = encryptor.encrypt_text(data)
root.clipboard_clear()
root.clipboard_append(encrypted_text.decode())
[Link]("Text Encrypted", f'Encrypted text copied to
clipboard.')
notif_window.destroy() # Close the notification window after encryption
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
# Handle decryption for files and text
def handle_decryption(data, notif_window, data_type):
if data_type == 'file':
decrypted_file_path = [Link]('.enc', '_decrypted')
encryptor.decrypt_file(data, decrypted_file_path)
[Link]("File Decrypted", f'Decrypted file saved to:
{decrypted_file_path}')
elif data_type == 'text':
try:
decrypted_text = encryptor.decrypt_text([Link]())
[Link]("Text Decrypted", f'Decrypted text:
{decrypted_text}')
except:
[Link]("Error", "Failed to decrypt text. Invalid input.")
notif_window.destroy()
if __name__ == "__main__":
# Generate or load encryption key
key_file = '[Link]'
if not [Link](key_file):
encryptor = FileEncryptor()
encryptor.save_key(key_file)
else:
key = FileEncryptor.load_key(key_file)
encryptor = FileEncryptor(key)
# Create the main application window
root = [Link]()
[Link]("Cryptography using Hash Functions")
[Link]("400x300")
[Link](bg="#2E3B4E") # Background color
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
# Add labels and buttons with professional colors
title_label = [Link](root, text="Cryptography using Hash Functions",
font=("Arial", 14, "bold"), bg="#2E3B4E", fg="white")
title_label.pack(pady=20)
btn_upload_file = [Link](root, text="Upload File", font=("Arial", 12),
bg="#FFC107", fg="black", command=upload_file)
btn_upload_file.pack(pady=10)
btn_input_text = [Link](root, text="Input Text", font=("Arial", 12),
bg="#FFC107", fg="black", command=input_text)
btn_input_text.pack(pady=10)
[Link]()
Encryption & Decryption:
The encryption process involves generating a hash digest for a given input, while
decryption verifies the integrity of the original message. Practical use cases include
file encryption and ensuring message integrity during transmission.
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Output:
Select Operation to perform from the Menu:
Text Encryption:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Text Decryption:
Select File to Encrypt or Decrypt:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
File Encryption:
File Decryption:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Encrypted File: Decrypted File:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Conclusion:
This project demonstrates how hash functions can be effectively used in
cryptography to secure different forms of data. By implementing encryption and
decryption methods, we provide a practical overview of their role in maintaining
data confidentiality and integrity.