|
| 1 | +''' |
| 2 | +Aim: Given a string S , encrypt or decrypt it as per the choice entered by the user using Caesar Cipeher Substitution Method |
| 3 | + |
| 4 | +Input Format: |
| 5 | +1. The first line provides an option to the user to choose either to encrypt/decrypt |
| 6 | +2. In the second line the user inputs the string as per his choice given in part 1. |
| 7 | +
|
| 8 | +Note : It is considered that the encrypted string consists of both upper and lowercase letters , while the decrypted strings consist of only uppercase letters. |
| 9 | +''' |
| 10 | + |
| 11 | +def decrypt(encrypted_string,key_val): |
| 12 | + '''This is the decryption function |
| 13 | + It takes in input encrypted string and key value |
| 14 | + Prints nothing and return the decrypted string value''' |
| 15 | + final = "" |
| 16 | + encrypted_string = encrypted_string.upper() |
| 17 | + for i in range(len(encrypted_string)): |
| 18 | + decrypt_char = ord(encrypted_string[i]) + key_val |
| 19 | + if (decrypt_char > 90): |
| 20 | + decrypt_char = 65 - ( 90 - decrypt_char) |
| 21 | + decrypt_char = chr(decrypt_char) |
| 22 | + decrypt_char = decrypt_char.lower() |
| 23 | + final += decrypt_char |
| 24 | + return final |
| 25 | + |
| 26 | + |
| 27 | +def encrypt(input_string,key_val): |
| 28 | + '''This is the encryption function |
| 29 | + It takes in input string and key value. |
| 30 | + Prints nothing and return the Encrypted string value''' |
| 31 | + final = "" |
| 32 | + input_string = input_string.upper() |
| 33 | + for i in range(len(input_string)): |
| 34 | + encrypt_char = ord(input_string[i]) + key_val |
| 35 | + if (encrypt_char > 90): |
| 36 | + encrypt_char = 65 - ( 90 - encrypt_char) |
| 37 | + encrypt_char = chr(encrypt_char) |
| 38 | + encrypt_char = encrypt_char.lower() |
| 39 | + final += encrypt_char |
| 40 | + return final |
| 41 | + |
| 42 | +#DRIVER CODE |
| 43 | + |
| 44 | +#This is the code for menu , chosing whether to encrypt or decrypt |
| 45 | +choose =int(input('CHOOSE FROM THE FOLLOWING : \n 1. Encrypt String \n 2. Decrypt String \n')) |
| 46 | + |
| 47 | +#This is the code for encryption choice , it calls the function and prints the encrypted value |
| 48 | +if (choose == 1): |
| 49 | + input_string = input('Enter the input text ') |
| 50 | + key_val = int(input('Enter the key value ')) |
| 51 | + print('Encrypted string is : ' + encrypt(input_string,key_val)) |
| 52 | + |
| 53 | +#This is the code for decryption choice , it calls the function and prints the decrypted value |
| 54 | +if (choose == 2): |
| 55 | + encrypted_string = input('Enter the encrypted text ') |
| 56 | + key_val = int(input('Enter the key value ')) |
| 57 | + print('Decrypted string is : ' + decrypt(encrypted_string,key_val)) |
| 58 | + |
| 59 | + ''' |
| 60 | + Sample Test Case : |
| 61 | + Case 1 : |
| 62 | + User chooses to input string and encrypt the inputted string. |
| 63 | + input : |
| 64 | + string : escrsce |
| 65 | + key value : 5 |
| 66 | + output : |
| 67 | + encrypted string : JXHxwhJ |
| 68 | + |
| 69 | + Case 2 : |
| 70 | + User chooses to enter an encrypted string and decrypt it. |
| 71 | + input : |
| 72 | + string : trWEcere |
| 73 | + key value : 6 |
| 74 | + output : |
| 75 | + Decrypted String : ZXDKIKXK |
| 76 | + |
| 77 | + Explaination : |
| 78 | + In this program , we take the input string and key value .Once we get the key value , we substitute the alphabet of the string with the updated key value alphabet. |
| 79 | + For eg . if input character is A and key value is 3 , so it will be subsituted by D ( A + 3 = D) |
| 80 | + Also in case if the encrypted string consists of both lower and uppercase characters we convert the whole string to uppercase |
| 81 | + |
| 82 | + ''' |
| 83 | + |
| 84 | + |
0 commit comments