Symmetric Encryption and Decryption Using Substition Cipher
Symmetric Encryption and Decryption Using Substition Cipher
PROJECT FILE ON
-----------------------------------------------------------
PR0JECT PREPARED BY :
1.
2.
3.
PRINCIPAL
ACKNOLEDGEMENT
I wish to express my profound
thanks to my institution NARAYANA E-TECHNO
SCHOOL,VELLORE for having enabled me to take
COMPUTER SCIENCE group in Class XII.
I would also like to express my gratitude to the
Management, Principal Mr. P JEEVARAJU for their
continuous support which enabled me to complete
my project successfully.
My sincere thanks to Mrs. Tamilselvi.S A guide,
Mentor all the above a friend, who critically
reviewed my project and helped in solving each and
every problem, occurred during implementation of
the project.
Last but not the least, I would like to thank my
family members and friends and all those who
helped me to complete my project successfully
BY
------------------------
Encryption and Decryption using a Substitution Cipher
TABLE OF CONTENTS
1 Certificate i
2 Acknowledgement ii
3 Introduction 2-7
4 Objective 8-9
5 Proposed system 10-14
6 Flowchart 15-17
7 Source code 18-20
8 Working principle 21
9 Execution 22-23
10 Future scope 24-28
11 Conclusion 29
12 Requirements 30
13 Bibliography 31
1|Page
Encryption and Decryption using a Substitution Cipher
INTRODUCTION
In today's increasingly digital world, encryption stands as one of the
most essential technologies to protect information. From securing online
banking transactions to safeguarding private communications, encryption
plays a vital role in ensuring the confidentiality, integrity, and authenticity
of data. With the rise of cyber threats and the growing importance of data
privacy, encryption has evolved into a cornerstone of modern information
security. This essay will explore the concept of encryption, its historical
evolution, types of encryption, and its significance in safeguarding
information.
Understanding Encryption:
Encryption is the process of converting plain, readable information
into an unintelligible format known as ciphertext. The goal of encryption is
to ensure that only authorized individuals, who possess the correct
decryption key, can access the original data. Without this key, the
encrypted information remains virtually unreadable, even if intercepted by
unauthorized parties. This mechanism is fundamental to maintaining the
privacy and security of sensitive data, such as personal information,
financial details, or classified government documents.
At the core of encryption lies the use of cryptographic algorithms,
which are mathematical functions designed to encode and decode data.
These algorithms employ complex calculations to scramble data, ensuring
that it is transformed in a way that is difficult to reverse without the
2|Page
Encryption and Decryption using a Substitution Cipher
appropriate key. The strength of encryption is determined by the
complexity of the algorithm used and the length of the encryption key. The
longer and more complex the key, the more secure the encryption.
A Brief History of Encryption:
The history of encryption dates back thousands of years. One of the
earliest known forms of encryption was used by the ancient Egyptians, who
developed hieroglyphic codes to protect sensitive information. Similarly,
the ancient Greeks utilized the scytale, a device that encoded messages
using a cylinder wrapped with parchment. The famous Roman general
Julius Caesar also employed a simple substitution cipher, known as the
Caesar cipher, to communicate securely with his commanders.
Throughout the centuries, encryption techniques evolved alongside
advancements in technology and mathematics. The development of more
sophisticated ciphers, such as the Vigenère cipher in the 16th century,
marked a significant leap forward. However, it was not until the advent of
computers in the 20th century that encryption truly transformed into a
complex and indispensable tool for securing communications.
During World War II, encryption played a critical role in military
operations, with both Allied and Axis powers relying on machines like the
German Enigma to encrypt their communications. The breaking of the
Enigma code by British mathematician Alan Turing and his team at
Bletchley Park is often credited with shortening the war by several years.
This period marked the beginning of modern cryptography, where
encryption became more closely tied to computational power and digital
communication.
3|Page
Encryption and Decryption using a Substitution Cipher
Types of Encryption:
There are two main types of encryption: symmetric encryption and
asymmetric encryption.
1. Symmetric Encryption:
In symmetric encryption, the same key is used for both encrypting and
decrypting the data. This type of encryption is fast and efficient, making it
suitable for encrypting large amounts of data. However, its primary
weakness lies in key distribution: both the sender and receiver must share
the same key, which can be risky if the key is intercepted during
transmission.
One common example of symmetric encryption is the Advanced
Encryption Standard (AES), which is widely used in a variety of applications,
including securing wireless networks and encrypting files on devices.
2. Asymmetric Encryption:
Asymmetric encryption, also known as public-key encryption, uses
two different keys: a public key for encryption and a private key for
decryption. The public key can be shared with anyone, while the private
key remains confidential to the owner. This method eliminates the problem
of key distribution and enhances security.
The most well-known example of asymmetric encryption is the RSA
algorithm, named after its inventors Rivest, Shamir, and Adleman. RSA is
commonly used in securing internet communications, including digital
signatures and SSL/TLS protocols for secure web browsing.
4|Page
Encryption and Decryption using a Substitution Cipher
Python Overview:-
Python is a general-purpose high-level programming language. It is an
open source language, released under a GPL-Compatible license. Python
Software Foundation (PSF), a non-profit organization, holds the copy-right
of python. Guido Van Rossum conceived python in the late 1980s. It was
released in 1991 at Centrum Wiskunde & Informatica (CWI) in the
Netherlands as a successor to the ABC language. He named this language
after a popular comedy show called „Monty Python‟s Flying Circus‟ (and
not after python - the snake). In the last few years, it‟s popularity has
increased immensely. According to stackoverflow.com‟s recent survey,
python is in the top ten most popular technologies in 2018. It is also
dynamically-typed because it carries out type-checking at run time. It does
so to make sure that the type of construct matches what we except it to
be. The distinctive feature of python is that it is an interpreted language.
The Python IDLE (Integrated Development & Learning Environment)
executes instruction one line at a time. The python programming language
is one of the richest languages.
Features of Python :-
1. Easy :-
Python is a very easy to learn and understand; using this python
tutorial, any beginner can understand the basics of python.
2. Interpreted :-
It is interpreted (executed) line by line. This makes it easy to test and
debug.
5|Page
Encryption and Decryption using a Substitution Cipher
3. Object Oriented :-
The python programming language supports classes and objects.
4. Free and Open-Source :-
The language and it‟s source code are available to the public for free;
there is no need to buy a costly license.
5. Portable :-
Since it is open source, we can run python on windows, mac, linux, or
any other platforms. Our programs will work without needing to the
changed for every machine.
6. GUI (Graphical User Interface) programming :-
We can use it to develop a GUI (Graphical User Interface). One way to
do this is through „Tkinter‟.
7. Large Library :-
Python provides us with a large standard library. We can use it to
implement a variety of functions without needing to reinvent the wheel
every time. Just pick the code we need and continue. This lets us to focus
on other important tasks.
6|Page
Encryption and Decryption using a Substitution Cipher
7|Page
Encryption and Decryption using a Substitution Cipher
OBJECTIVES
Symmetric encryption is a cryptographic method where the same
key is used for both encryption and decryption of data. The main
objectives of implementing simple symmetric encryption using Python
are:
1. Understanding the Core Concept of Encryption: Help students grasp
the fundamental idea of encryption—transforming readable data
(plaintext) into unreadable data (ciphertext) and back to plaintext
using a key.
2. Learning Symmetric Key Mechanism: Introduce the concept of
symmetric keys, where the same key is used for both encrypting and
decrypting the data, allowing students to see how security is
maintained with key management.
3. Hands-on Coding with Basic Encryption Algorithms: Provide
students with practical experience in using Python to implement
basic symmetric encryption techniques like the Caesar cipher or XOR
cipher, which demonstrate how simple transformations secure data.
4. Thinking: By writing Developing Logical code for encryption and
decryption, students enhance their logical thinking and problem-
solving skills as they map each character to its encrypted
counterpart.
8|Page
Encryption and Decryption using a Substitution Cipher
5. Understanding the Role of Encryption in Security: Illustrate how
encryption is crucial in protecting sensitive data, helping students
appreciate the importance of encryption in cybersecurity, data
privacy, and secure communications.
9|Page
Encryption and Decryption using a Substitution Cipher
10 | P a g e
Encryption and Decryption using a Substitution Cipher
Decryption Module: Decrypts the ciphertext using the stored key
(read from the file) by reversing the substitution process. The
decrypted message should match the original plaintext.
File I/O Module: Handles reading plaintext from files, writing
encrypted text to files, and ensuring that file paths are managed
properly.
2. Flow of the System
The system operates in a simple sequential flow:
1. Key Generation: A unique substitution key is generated using a
shuffled list of printable ASCII characters. This key maps each
character in the input to a random character in the output.
2. Key Storage: Once the key is generated, it is serialized and saved in a
file (in binary format) using pickle. This allows for retrieval later
during decryption.
3. Encryption: The encryption function reads the input text (either
from a file or directly from user input) and uses the stored
substitution key to convert each character of the plaintext into its
encrypted counterpart. The resulting ciphertext is stored in a file.
4. Decryption: During decryption, the user provides the path to the
encrypted file and the key file. The decryption process retrieves the
key, reverses the substitution mapping, and restores the original
plaintext. This plaintext can either be printed or saved in a new file.
11 | P a g e
Encryption and Decryption using a Substitution Cipher
12 | P a g e
Encryption and Decryption using a Substitution Cipher
Encryption Module:
Objective: To convert plaintext into ciphertext using the substitution
key.
Functionality:
o Reads text from a file or takes direct user input.
o Converts each character of the text into its encrypted
counterpart using the substitution cipher.
o Writes the encrypted text to a file.
Decryption Module:
Objective: To convert ciphertext back into plaintext using the stored
key.
Functionality:
o Reads the encrypted text from a file.
o Loads the substitution cipher key from a stored file using pickle.
o Reverses the substitution by swapping keys and values in the
dictionary, and converts ciphertext back to plaintext.
o Outputs the decrypted text.
File I/O Module;
Objective: To handle file operations for reading and writing
encrypted/decrypted text.
Functionality:
o Ensures that text can be read from input files and written to
output files.
o Handles error conditions like missing or inaccessible files.
13 | P a g e
Encryption and Decryption using a Substitution Cipher
o Allows the user to provide custom file paths for inputs and
outputs.
4. System Flow (User Interaction)
Here’s how a typical user interaction with the system would look:
1. User Input for Encryption:
o The user is prompted to select either to:
Encrypt a file,
Encrypt direct user input, or
Decrypt an existing file.
o If the user chooses to encrypt:
The system generates a new substitution key.
The user provides the file path for saving the key.
The user provides the text to be encrypted (either from a
file or manual input).
The encrypted text is written to a specified file.
2. User Input for Decryption:
o The user is prompted to provide the file path of the encrypted
file and the stored key.
o The system reads both the encrypted text and the stored key.
o The ciphertext is decrypted using the reverse mapping of the
substitution key.
o The original plaintext is displayed or saved to a new file.
14 | P a g e
Encryption and Decryption using a Substitution Cipher
FLOWCHART
15 | P a g e
Encryption and Decryption using a Substitution Cipher
16 | P a g e
Encryption and Decryption using a Substitution Cipher
o Write Encrypted File: The encrypted text is saved to an output
file.
o Save Key: The encryption key is stored in a file.
5. Action 3: Decrypt File:
o Read Encrypted File: The program reads the content of the
specified encrypted file.
o Load Key: The program loads the key (previously saved during
encryption) from a file.
o Decrypt Text: The program decrypts the encrypted content
using the key.
o Display Decrypted Text: The decrypted (original) text is
displayed to the user.
6. End:
o The process ends after completing the selected operation.
“Each action has its own distinct steps, ensuring that files are either
encrypted, stored, or decrypted based on the user’s input.”
17 | P a g e
Encryption and Decryption using a Substitution Cipher
SOURCE CODE
import random
import pickle
#key creation
def keycreation():
l1 = []
for i in range(32,127):
l1.append(chr(i))
random.shuffle(l1)
l2 = []
for i in range(32,127):
l2.append(chr(i))
random.shuffle(l2)
dal = dict(zip(l1,l2))
return dal
dal = keycreation()
def keystore(fpath):
sec = open(fpath,'wb')
pickle.dump(dal,sec)
sec.close()
#encrytion
def encryption(text):
etxt = ''
for a in text.lower():
entext = dal.get(a)
etxt += entext
return (etxt)
#key reversing
def keymap(fpath):
18 | P a g e
Encryption and Decryption using a Substitution Cipher
sec = open(fpath,'rb')
dal = pickle.load(sec)
return dal
#decrypting
def decryption(text):
revkey = {}
for key, value in dal.items():
revkey[value] = key
dtxt = ''
for a in text:
detext = revkey.get(a)
dtxt += detext
return dtxt
def wtxtfile(fpath,etxt):
f = open(fpath,'w')
f.write(etxt)
if action == 1:
inloc=eval(input('enter the file input location:'))
text = rtxtfile(inloc)
etxt=encryption(text)
wrloc=eval(input('enter the file output location:'))
19 | P a g e
Encryption and Decryption using a Substitution Cipher
wtxtfile(wrloc,etxt)
keyloc=eval(input('enter the location to store key:'))
keystore(keyloc)
elif action == 2:
text = input('enter the text you want to encrypt:')
etxt = encryption(text)
wrloc = eval(input('enter the file output location:'))
wtxtfile(wrloc, etxt)
keyloc = eval(input('enter the location to store key:'))
keystore(keyloc)
elif action == 3:
inloc = eval(input('enter the file location to decrypt:'))
text = rtxtfile(inloc)
keyloc = eval(input('enter the location of key:'))
dal = keymap(keyloc)
print(decryption(text))
else:
print('please enter a valid action')
#The END
20 | P a g e
Encryption and Decryption using a Substitution Cipher
WORKING PRINCIPLE
Example flow for encryption:
1. Input:
o User selects the action to "Encrypt from a file".
o User provides the path to the plaintext file: message.txt.
o User provides the path to store the encryption key: keyfile.bin.
o User provides the path to store the encrypted text: encrypted_message.txt.
2. Process:
o A substitution cipher key is generated and stored in keyfile.bin.
o The plaintext from message.txt is encrypted and saved to encrypted_message.txt.
3. Output:
o Encrypted message is saved, and the key is securely stored for future decryption.
Example flow for decryption:
1. Input:
o User selects the action to "Decrypt an already encrypted file".
o User provides the path to the encrypted file: encrypted_message.txt.
o User provides the path to the key file: keyfile.bin.
2. Process:
o The key from keyfile.bin is loaded.
o The encrypted text from encrypted_message.txt is decrypted using the reverse
mapping of the key.
3. Output:
o The decrypted text is displayed or saved to a new file.
21 | P a g e
Encryption and Decryption using a Substitution Cipher
EXECUTION
MODE 1:
Now we can clearly see that the output is ciphered, and even the unique key
for it is stored in the form of binary ,which makes it unreadable for the user.
22 | P a g e
Encryption and Decryption using a Substitution Cipher
MODE 2:
MODE 3 (Decryption):
We can clearly see that the ciphered text has been converted into user readable form
.Here we have used dictionaries the pair the key and letter pair, this dictionary is stored in
binary form as the key, by accessing this only we can decipher the text. there are about a
billion unique combination of keys.
23 | P a g e
Encryption and Decryption using a Substitution Cipher
Future scope
While the current system provides a basic understanding of encryption and decryption
using a substitution cipher, it can be significantly enhanced in terms of security, usability, and
performance. Below, we will outline several ways to improve the system along with future
scopes that can expand its functionality.
1. Security Improvements:
The substitution cipher is a simple encryption technique, but it is vulnerable to attacks
like frequency analysis. To make the system more secure, here are some improvements:
•Use Stronger Encryption Algorithms:
O Upgrade from substitution cipher to modern cryptographic algorithms like AES
(Advanced Encryption Standard) or RSA. These algorithms are widely used for secure
communications and provide far stronger encryption.
O Implement Hybrid Encryption, combining AES (for encrypting the message) and RSA
(for encrypting the key), to ensure better security.
•Key Management:
O Password-Protected Key Storage: Instead of storing the key in plaintext in a file,
encrypt the key file itself using a password-based encryption method (e.g., PBKDF2 with AES).
This ensures that even if the key file is stolen, it can't be used without the password.
O Key Rotation: Implement a system for periodic key rotation to enhance security. This
can be important for systems that process large amounts of data or require long-term security.
• Salting and Hashing:
O Implement salting and hashing mechanisms to further protect the keys and user
credentials. The key, for instance, could be hashed and salted before being stored.
• Authentication and Authorization:
O Introduce authentication mechanisms to ensure that only authorized users can
perform encryption or decryption.
O Implement access control to limit the operations based on user roles, especially when
sensitive data is being encrypted.
24 | P a g e
Encryption and Decryption using a Substitution Cipher
25 | P a g e
Encryption and Decryption using a Substitution Cipher
• Multi-threading or Parallel Processing:
O Leverage Python's concurrent futures or multiprocessing libraries to implement
parallel processing for encryption and decryption. This can improve performance on systems
with multiple cores.
• Optimize Key Lookup:
O If performance bottlenecks are detected in the key lookup process during
encryption/decryption, consider optimizing it with hash tables or trie structures that provide
faster access times.
• Compression Before Encryption:
O Implement file compression (e.g., using gzip or zip libraries) before encryption to
reduce the size of the data being encrypted. This can speed up both encryption and
transmission.
4. Feature Enhancements and Future Scopes:
In the future, the system can be expanded to provide additional functionalities, improving its
versatility and making it applicable to a broader range of use cases.
a) Hybrid Encryption:
• Public-Key Cryptography:
O Implement a system that uses public-key cryptography for sharing the encryption key
securely (e.g., RSA). For example, encrypt the data with a symmetric key (AES), then encrypt
the symmetric key with a public key (RSA) and share that alongside the data.
O This can allow secure communication between multiple parties without needing to
share secret keys manually.
b) Encryption Modes:
• Different Modes of Operation:
O Introduce various encryption modes such as ECB (Electronic Codebook), CBC (Cipher
Block Chaining), CFB (Cipher Feedback), or GCM (Galois/Counter Mode) for encryption. These
modes add randomness and improve the security of block ciphers like AES.
26 | P a g e
Encryption and Decryption using a Substitution Cipher
c) Data Encryption for Files and Media:
• File Encryption for Binary Files:
O Extend the system to handle encryption and decryption of binary files such as images,
videos, and PDFs. This would require modifying the file reading and writing methods to work in
binary mode (using 'rb' and 'wb' modes).
d) Cloud Integration:
• Cloud-Based Key Management:
O Integrate the system with cloud services (e.g., AWS Key Management Service, Azure
Key Vault, or Google Cloud KMS) to securely store encryption keys and manage encryption at
scale. This can enable key distribution and management for larger systems.
• Cloud Storage Encryption:
O Enable integration with cloud storage platforms like AWS S3, Google Drive, or Dropbox
to encrypt files before uploading them. This ensures that sensitive files stored in the cloud
remain secure.
e) Encryption of Databases:
• Database Encryption:
O Extend the system to encrypt sensitive fields in databases (e.g., passwords, credit card
information) using strong encryption methods like AES. This could be achieved using ORMs
(Object-Relational Mappers) like SQLAlchemy or Django ORM to apply encryption at the
application level.
f) Mobile and IoT Integration:
• Mobile App Development:
O Develop a mobile version of the encryption system to allow users to encrypt messages
or files directly from their smartphones. This could involve using Kivy for Python-based mobile
app development or porting the code to languages like Swift (for iOS) or Kotlin (for Android).
• IoT Encryption:
O Implement lightweight encryption techniques suitable for Internet of Things (IoT)
devices. IoT devices often require encryption for secure communication due to their sensitive
nature (e.g., in healthcare, smart homes).
27 | P a g e
Encryption and Decryption using a Substitution Cipher
Limitations:
The key must be preserved for future uses, no recovery system
was set for keys.
28 | P a g e
Encryption and Decryption using a Substitution Cipher
CONCLUSION:
By enhancing the system with these improvements and expanding
its scope, you can create a more robust, secure, and versatile encryption
tool. The core substitution cipher provides a foundation for
understanding encryption concepts, but as encryption needs evolve,
integrating advanced cryptographic techniques, security measures, and
usability features will make the system more practical for real-world
applications. The future of this system lies in its scalability, ability to
handle a wider range of use cases (like file encryption, cloud storage, and
mobile app encryption), and preparing for emerging technologies like
quantum computing.
This system provides a straightforward approach to text encryption
and decryption using symmetric substitution ciphers. It demonstrates the
core concepts of key generation, encryption, key storage, and decryption
in a manner that can be easily understood and applied. Although simple,
the system serves as a foundational learning tool for understanding
encryption mechanics, and with the proposed improvements, it can be
made more robust and secure.
29 | P a g e
Encryption and Decryption using a Substitution Cipher
Requirements
30 | P a g e
Encryption and Decryption using a Substitution Cipher
Bibliography
31 | P a g e
Encryption and Decryption using a Substitution Cipher
THANK
YOU
32 | P a g e