-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.py
35 lines (25 loc) · 999 Bytes
/
Encryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Python program to encrypt and decrypt a message
import random
import string
chars = " " + string.punctuation + string.ascii_letters + string.digits
chars = list(chars)
key = chars.copy()
random.shuffle(key)
#print(f"Characters: {chars}")
#print(f"Key: {key}")
original_text = input("Enter the message: ")
cipher_text = ""
#Encryption
for letter in original_text:
index = key.index(letter) #gets the index from "key" list
cipher_text += chars[index] #appends cipher_text with corresponding element from "chars" list
print(f"Original text: {original_text}")
print(f"Cipher text: {cipher_text}")
cipher_text = input("Enter the encrypted message: ")
original_text = ""
#Decryption
for letter in cipher_text:
index = chars.index(letter) #gets the index from "chars" list
original_text += key[index] #appends original_text with corresponding element from "key" list
print(f"Cipher text: {cipher_text}")
print(f"Original text: {original_text}")