0% found this document useful (0 votes)
538 views25 pages

Python Image & Message Encryption Project

This document is a project report on message and image encryption and decryption using Python. It discusses encrypting and decrypting messages and images in Python using techniques like converting images to byte arrays and applying XOR operations. The objectives are to provide security for messages and images to prevent unauthorized access and illegal copying. The process involves selecting an image, encrypting it by converting it to numeric form and applying XOR with a key, and decrypting using the same key.

Uploaded by

Mohd Farman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
538 views25 pages

Python Image & Message Encryption Project

This document is a project report on message and image encryption and decryption using Python. It discusses encrypting and decrypting messages and images in Python using techniques like converting images to byte arrays and applying XOR operations. The objectives are to provide security for messages and images to prevent unauthorized access and illegal copying. The process involves selecting an image, encrypting it by converting it to numeric form and applying XOR with a key, and decrypting using the same key.

Uploaded by

Mohd Farman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A Project Report

on

MESSAGE AND IMAGE ENCRYPTION AND DECRYPTION

Submitted in partial fulfilment of the requirement for the award of the

certificate of

PYTHON PROGRAMMING

SUBMITTED TO:
Mr. Mayur Dev Sewak
General Manager, Operations
Eisystems Services
Ms. Mallika Srivastava
Trainer, Data Science & Analytics Domain
Eisystems Services

SUBMITTED BY:

Mohd Farman

1
index

Serial no. Page no.

1 Cover Page
1
2 Acknowledge 3

3 Python 4

4 Encrypt and Decrypt 5


message and image using
python
Decryption 6

5 Abstract 9

6 Objective 10

7 Details of process 11

8 System requirement 18

9 Data flow diagram 19

10 The Ou 23

11 Summary 24

12 References 25

2
ACKNOWLEDGE

I would like to acknowledge the contributions of the following people without whose help
and guidance this report would not have been completed. I acknowledge the counsel and
support of our training coordinator, Ms. Mallika Srivastava Trainer, Data Science & Analytics
Domain Eisystems Services with respect and gratitude, whose expertise, guidance, support,
encouragement, and enthusiasm has made this report possible. Their feedback vastly
improved the quality of this report and provided an enthralling experience. I am indeed
proud and fortunate to be supported by her.
This acknowledgement will remain incomplete if I fail to express our deep sense of
obligation to my parents and God for their consistent blessings and encouragement.

3
Introduction

Python –The New Generation Language

Python is a widely used general-purpose, high level programming language. It was initially
designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It
was mainly developed for an emphasis on code readability, and its syntax allows
programmers to express concepts in fewer lines of code. Python is dynamically typed and
garbage-collected. It supports multiple programming paradigms, including procedural,
object-oriented, and functional programming. Python is often described as a "batteries
included" language due to its comprehensive standard library.

Features

• Interpreted

In Python there is no separate compilation and execution steps like C/C++. It directly run
the program from the source code. Internally, Python converts the source code into an
intermediate form called bytecodes which is then translated into native language of specific
computer to run it.

• Platform Independent

Python programs can be developed and executed on the multiple operating system
platform. Python can be used on Linux, Windows, Macintosh, Solaris and many more.

• Multi- Paradigm

Python is a multi-paradigm programming language. Object-oriented programming and


structured programming are fully supported, and many of its features support functional
programming and aspectoriented programming .

• Simple

Python is a very simple language. It is a very easy to learn as it is closer to English language.
In python more emphasis is on the solution to the problem rather than the syntax.

• Rich Library Support

Python standard library is very vast. It can help to do various things involving regular
expressions, documentation generation, unit testing, threading, databases, web browsers,
CGI, email, XML, HTML, WAV files, cryptography, GUI and many more.

• Free and Open Source

4
Firstly, Python is freely available. Secondly, it is open-source. This means that its source
code is available to the public. We can download it, change it, use it, and distribute it. This is
called FLOSS (Free/Libre and OpenSource Software). As the Python community, we’re all

headed toward one goal- an ever-bettering Python.

Encrypt and Decrypt message and image using python

Encryption is the act of encoding a message so that only the intended users can see it. We
encrypt data because we don’t want anyone to see or access it.
We will use the cryptography library to encrypt a file. The cryptography library uses a
symmetric algorithm to encrypt the file. In the symmetric algorithm, we use the same key to
encrypt and decrypt the file. The fernet module of the cryptography package has inbuilt
functions for the generation of the key, encryption of plain text into cipher text, and
decryption of cipher text into plain text using the encrypt() and decrypt() methods
respectively. The fernet module guarantees that data encrypted using it cannot be further
manipulated or read without the key.

Encryption
It is nothing but a simple process in which we convert our data or information into secret
code to prevent it from unauthorized access and keep it private and secure.
First, we will select an image, and then we will convert that image into a byte array due to
which the image data will be totally converted into numeric form, and then we can easily
apply the XOR operation to it. Now, whenever we will apply the XOR function on each value
of the byte array then the data will be changed due to which we will be unable to access it.
But we should remember one thing here our encryption key plays a very important role
without that key we can not decrypt our image. It acts as a password to decrypt it.
The below program depicts the basic approach to encryption:
# Assign values
data = 1281
key = 27

# Display values
print('Original Data:', data)
print('Key:', key)

5
# Encryption
data = data ^ key

print('After Encryption:', data)

# Decryption
data = data ^ key
print('After Decryption:', data)

Output:

Original Data: 1281


Key: 27
After Encryption: 1306
After Decryption: 1281

Decryption
It is nothing but a process of converting our encrypted data into a readable form. Here
we will again apply the same XOR operation on an encrypted image to decrypt it. But always
remember that our encryption key and decryption key must be the same.
Executable Code for Decryption:

# try block to handle the exception


try:

# take path of image as a input


path = input(r'Enter path of Image : ')

# taking decryption key as input


key = int(input('Enter Key for encryption of Image : '))

# print path of image file and decryption key that we are using

6
print('The path of file : ', path)
print('Note : Encryption key and Decryption key must be same.')

print('Key for Decryption : ', key)

# open file for reading purpose


fin = open(path, 'rb')

# storing image data in variable "image"


image = [Link]()
[Link]()

# converting image into byte array to perform decryption easily on numeric data
image = bytearray(image)

# performing XOR operation on each value of bytearray


for index, values in enumerate(image):
image[index] = values ^ key

# opening file for writing purpose


fin = open(path, 'wb')

# writing decryption data in image


[Link](image)
[Link]()
print('Decryption Done...')

except Exception:
print('Error caught : ', Exception.__name__)

7
Output:

Enter path of Image : C:\Users\lenovo\Pictures\Instagram\[Link]


Enter Key for Decryption of Image : 22
The path of file : C:\Users\lenovo\Pictures\Instagram\[Link]
Note : Encryption key and Decryption key must be same.
Key for Decryption : 22
Decryption done...

8
Abstract

In this project we have created a Python programming Project named “Message and Image
Encryption and Decryption”. An Image Encryption Decryption is an image processing
application created in python with tkinter gui and OpenCv library.
In this application user can select an image and can encrypt that image to gray scale image
and can even decrpyt also.
The purpose of encryption is confidentiality concealing the content of the message by
translating it into a code.
Also after encrypting and decrypting user can also save the edited image anywhere in the
local system.
Also there is option to reset to the original image.

9
Objectives
The main objective of our project is to provide security of the message and image-based
data with the help of suitable key and protect the message and image from illegal copying
and distribution

10
Detail of process

Introduction

Python Message and Image Encryption Decryption Project

In this digital era, the need for security is increasing rapidly. Complying with this requirement,
the encryption & decryption algorithms were devised.

Now, we will build a project that can encode and decode a message. Let’s start by getting to
know more about this project.

Message and Image Encryption Decryption Project in Python


Encoding is the process of converting text into an incognizable language and the reverse
process in decoding. In this project, we will be using the Tkinter module and base64 module
to do the required operations.

[Link] user first enters the message and key


[Link] choose one of the encode or decode options
[Link] on the choice the corresponding operation is done
[Link] is to be noted that the same key used for encryption needs to be used during the
decryption.
Project Prerequisites

To build this project, the developer needs to have good knowledge of the Tkinter module and
Python. It is also advised to have some idea on encryption & decryption.

Python Message Encryption Decryption Project File Structure


Steps for building the python message encryption decryption project:

1. Installing the required modules


2. Importing the modules

11
3. Writing function for encryption
4. Writing function for decryption

5. Creating the window


6. Adding the input and output components
7. Adding the buttons and their functions

1. Installing the required modules


The base64 is available by default. If it is not you can install is using the command

pip install base64

The Tkinter module needs to be installed. You can install this module by using the following
command.

pip install tkinter

2. Importing the modules


The first step is to import the required modules. Here we will be using the tkinter module for
building the GUI and the base64 module for encryption and decryption purposes.

3. Writing function for encryption

12
Code Explanation:
a. ord(): This function gives the corresponding ASCII values of a given character.

b. chr(): This function gives the corresponding character to the given ASCII value.
c. append(): Adds the elements to the end of the list
d. ord(msg[i]) + ord(list_key)) % 256 : This gives the remainder of division of addition of
ord(message[i]) and ord( key_c) with 256 and passes that remainder to chr() function
e. base64.urlsafe_b64encode(): This helps in encoding the string using url and file system safe
alphabets into the binary form.

4. Writing function for decryption

Code Explanation:
a. base64.urlsafe_b64decode(): This helps in decoding the binary string using url and file
system safe alphabets into the normal form of strings.
b. ord(): This function gives the corresponding ASCII values of a given character
c. chr(): This function gives the corresponding character to the given ASCII value
d. 256 + ord(message[i]) – ord(key_c)) % 256 : This gives the remainder of addition of 256 with
subtraction of ord(message[i]) – ord( key_c) and then division with 256 and passes that
remainder to chr() function

e. append(): Adds the elements to the end of the list

13
5. Creating the window

Code Explanation:
a. geometry(): It sets the length and width of the project window.
b. configure(): It sets the background color of the screen
c. title(): It displays the title on the top of the python message encryption-decryption project
window.

6. Adding the input and output components

14
Code Explanation:

a. The message and key hold the values in the entries that hold the input message key. And
mode checks which radio button is pressed for encryption/decryption. Output values hold the
value shown in the Result entry after doing the operation.
b. Frame(): It creates a rectangular object that holds various widgets on the screen
c. place(): This is used to place the widgets in a specified location based on coordinates or
relative to the parent component
d. Label(): This creates a label that can be used to show information
e. Entry(): This widget is used to take input from the user
f. Radiobutton(): These help in choosing one of the given options

15
7. Adding the buttons and their functions

Code Explanation:
a. The result() function

i. it takes the message, key, and the mode that are inputted by the user
ii. Then based on the mode, calls one of the encryption() or decryption() functions
iii. Shows the output in the Result field
iv. If no mode is selected, shows the message to select one of the either

16
b. Button(): This creates a button with mentioned color, text, etc. and the command
parameter represents the function that is to be executed on clicking the button

c. place(): This is used to place the widgets in a specified location based on coordinates or
relative to the parent component

d. mainloop(): This makes sure the screen runs in a loop till it is manually closed by the user

17
System Requirement
For user:

Browsers:
Google Chrome (recommended), Internet Explorer, Mozilla Firefox, Safari, Micro-softedge,
Opera, Vivaldi, etc.
Gamil account
High Speed Internet Connection
For Developers:
Operating system:
-Windows: XP/Vista/7/8/8.1/10 or late
–OSX: Snow leopard 10.6.3 or later Ubuntu, Debian, Fedora, Cent OS or Suse Linux

18
Data Flow Diagram

19
Input/output

20
21
The Output of Python Message Encryption Decryption Project
Fig1. The image of output of python encryption

22
Fig2. The image of output of python decryption

It can be observed that when the output of encryption of mohd farman is given as input to
decryption, the output is mohd farman.

23
Summary
In this project, we successfully built the python message encryption decryption system using
the Tkinter and base4 modules.

24
References

 [Link]

 [Link]

 [Link]

 [Link]

 [Link]

25

Common questions

Powered by AI

In the encryption project, the base64 module is used to encode and decode messages, transforming text into a format suitable for safe transmission and storage, while Tkinter provides a user interface to interact with these functions. Users input their text and key through the Tkinter interface, which then calls the base64 module to perform the encoding or decoding as per the chosen operation. This integration allows users to easily perform and view encryption or decryption results through a GUI, making the process user-friendly and straightforward .

The Python standard library supports encryption and decryption projects by providing modules like cryptography and libraries for handling input/output operations, GUI development, and data manipulation. These tools allow developers to implement complex functionalities effortlessly without needing to build them from scratch, which accelerates development time and increases code reliability. Furthermore, Python's 'batteries-included' philosophy provides comprehensive support for various tasks within the same ecosystem, reducing external dependencies and integration complexity .

Choosing a suitable key is paramount to the encryption process because it directly determines the security and confidentiality of the data. A well-chosen key that is sufficiently complex ensures that unauthorized decryption is extremely hard to accomplish, as brute force attacks or guessing would require computational power that is impractical. The key's uniqueness and unpredictability are vital, as using simple or short keys can compromise data security by making it easier to reverse-engineer the encryption process .

Tkinter provides several advantages in the Python based encryption-decryption project, including a straightforward way to build graphical user interfaces (GUIs), which simplify user interaction by allowing users to input data through forms rather than command-line inputs. Its widgets, such as buttons and entry fields, make it easier to select operations like encryption or decryption and enhance the user experience by providing a more accessible and friendly interface .

The Fernet module in Python's cryptography library ensures data integrity by providing inbuilt functions for key generation, encryption, and decryption that safeguard against manipulation. Fernet guarantees that encrypted data is authentic and has not been tampered with because encryption and decryption require the same key. This module assures that without the key, data cannot be read or modified, upholding both confidentiality and integrity .

Python’s dynamic typing feature benefits encryption-decryption programs by allowing for greater flexibility and simplicity in manipulating different data types without requiring explicit declarations. This enables the handling of various data types, such as strings and bytearrays, more efficiently within the same scope, simplifying the process of transformation and manipulation, which is crucial in cryptographic operations. It speeds up development time and reduces the likelihood of errors associated with type mismatch .

The XOR operation ensures data security by performing a bitwise operation between the data and a key, transforming the data into an unintelligible form. This operation is computationally simple yet effective, as the process can only be reversed (decrypted) by applying the XOR operation again with the same key. This symmetry does not reveal any original pattern of the data without the correct key, thus maintaining confidentiality .

A developer might face challenges such as handling input/output file errors, ensuring the correct key is always used, and managing exceptions during byte-level operations. They could overcome these by implementing robust error handling with try-except blocks, ensuring user input validation for both file paths and keys, and thoroughly testing the code with various data types and sizes. Additionally, providing detailed documentation and clear instructions for user inputs could reduce errors in operations .

The key is critical in maintaining the security of image encryption and decryption because it acts as a password that allows the encoded data (either image or message) to be reverted to its original form. In the XOR-based process described, the key is used to alter the byte array of the image during encryption and is required again to decipher the encrypted data back to its readable state. Without the correct key, it is computationally infeasible to decrypt the data successfully, as the original numeric values used in the XOR operation cannot be determined .

Using symmetric encryption for messages and images implies that the same key must be used for both encrypting and decrypting data, making key management critically important. While symmetric encryption is generally faster and less resource-intensive, it presents a security challenge in securely distributing and storing the key, since anyone with access to the key can decrypt the data. Ensuring the key's safety is essential to prevent unauthorized access; thus, it might be beneficial to use additional security measures such as key rotation and encryption together with key management protocols .

You might also like