Skip to content

Commit 9bcc766

Browse files
Merge pull request #16 from Namyalg/Substitution-Cipher
Substitution cipher
2 parents f17a721 + daff606 commit 9bcc766

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

Substitution_Cipher/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Substitution Cipher
2+
3+
- The Substitution Cipher is a Cryptographic technique used for the encryption of a message
4+
- The encryption is done based on a key agreed upon by the sender and receiver of the message
5+
- The letter is encoded/mapped to a different letter/character
6+
7+
## Working
8+
9+
- The user is prompted to enter the message as well as the key according to which the message will be encrypted
10+
- The key will be accepted as a CSV file
11+
- Considering an example :
12+
13+
![Image](assets/key.PNG)
14+
15+
- This is the key as agreed upon both the parties
16+
- On running the script, the user is prompted to enter a message
17+
18+
![Image](assets/encrypt.PNG)
7.19 KB
Loading

Substitution_Cipher/assets/key.PNG

3.86 KB
Loading

Substitution_Cipher/assets/key.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a,w
2+
b,e
3+
c,2
4+
d,q
5+
t,z
6+
y,4
7+
m,s
8+
f,1
9+
o,l
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#This script implements Substitution Cipher
2+
#The key for encrypting the message must be passed as a Command line argument in the form of a csv file
3+
4+
#To run the script from terminal, run
5+
#python3 substitution_cipher.py "path_to_csv_file_containing_the_key"
6+
7+
import csv
8+
import sys
9+
10+
def get_key():
11+
key = dict()
12+
with open(sys.argv[1] , 'r') as file:
13+
reader = csv.reader(file)
14+
for row in reader:
15+
key[str(row[0])] = str(row[1])
16+
return(key)
17+
18+
def encrypt():
19+
key = get_key()
20+
message = input("Enter the message ")
21+
encrypted_message = ''
22+
for letter in message:
23+
try:
24+
encrypted_message += key[letter]
25+
except:
26+
alert = "Your key does not have a suitable match for " + letter + "\nPlease update your key and try again!"
27+
return(-1, alert)
28+
return(0, encrypted_message)
29+
30+
if __name__ == "__main__":
31+
exit_code, message = encrypt()
32+
if (exit_code == 0):
33+
print("The encrypted message is " , message)
34+
else:
35+
print(message)

0 commit comments

Comments
 (0)