1
+ from random import choice
2
+
3
+ def decrypt (encrypted_string ,key_val ):
4
+ '''This is the decryption function
5
+ It takes in input encrypted string and key value
6
+ Prints nothing and return the decrypted string value'''
7
+ final = '"'
8
+ encrypted_string = encrypted_string .upper ()
9
+ for i in range (len (encrypted_string )):
10
+ decrypt_char = ord (encrypted_string [i ]) + key_val
11
+ if (decrypt_char > 90 ):
12
+ decrypt_char = 65 - ( 90 - decrypt_char )
13
+ decrypt_char = chr (decrypt_char )
14
+ final += decrypt_char
15
+ return final
16
+
17
+
18
+ def encrypt (input_string ,key_val ):
19
+ '''This is the encryption function
20
+ It takes in input string and key value.
21
+ Prints nothing and return the Encrypted string value'''
22
+ final = ""
23
+ input_string = input_string .upper ()
24
+ for i in range (len (input_string )):
25
+ encrypt_char = ord (input_string [i ]) + key_val
26
+ if (encrypt_char > 90 ):
27
+ encrypt_char = 65 - ( 90 - encrypt_char )
28
+ encrypt_char = chr (encrypt_char )
29
+ final += encrypt_char
30
+ final = '' .join (choice ((str .upper , str .lower ))(char ) for char in final )
31
+ return final
32
+
33
+ #DRIVER CODE
34
+
35
+ #This is the code for menu , chosing whether to encrypt or decrypt
36
+ choose = int (input ('CHOOSE FROM THE FOLLOWING : \n 1. Encrypt String \n 2. Decrypt String \n ' ))
37
+
38
+ #This is the code for encryption choice , it calls the function and prints the encrypted value
39
+ if (choose == 1 ):
40
+ input_string = input ('Enter the input text ' )
41
+ key_val = int (input ('Enter the key value ' ))
42
+ print ('Encrypted string is : ' + encrypt (input_string ,key_val ))
43
+
44
+ #This is the code for decryption choice , it calls the function and prints the decrypted value
45
+ if (choose == 2 ):
46
+ encrypted_string = input ('Enter the encrypted text ' )
47
+ key_val = int (input ('Enter the key value ' ))
48
+ print ('Decrypted string is : ' + decrypt (encrypted_string ,key_val ))
49
+
50
+
0 commit comments