DSProject Report
DSProject Report
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.
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
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 cryptography.fernet 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()
self.key = key
self.cipher_suite = Fernet(self.key)
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
@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 = file.read()
hasher.update(buf)
return hasher.hexdigest()
def input_text():
text = simpledialog.askstring("Input Text", "Enter text to encrypt or decrypt:")
if text:
display_notification(None, text)
if file_path:
encrypt_button = tk.Button(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
notif_window.mainloop()
notif_window.destroy()
if __name__ == "__main__":
# Generate or load encryption key
key_file = 'secret.key'
if not os.path.exists(key_file):
encryptor = FileEncryptor()
encryptor.save_key(key_file)
else:
key = FileEncryptor.load_key(key_file)
encryptor = FileEncryptor(key)
root.mainloop()
Output:
Text Encryption:
AGNEL INSTITUTE OF TECHNOLOGY AND DESIGN
AGNEL TECHNICAL EDUCATIONAL COMPLEX
ASSAGAO, BARDEZ-GOA. 403 507
DEPARTMENT OF COMPUTER ENGINEERING
Text Decryption:
File Encryption:
File Decryption:
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.