File tree Expand file tree Collapse file tree 5 files changed +62
-0
lines changed Expand file tree Collapse file tree 5 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 + "\n Please 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 )
You can’t perform that action at this time.
0 commit comments