Random Substitution Cipher in Python Description
This project is a Python program that encrypts and decrypts messages using a random substitution cipher.
A substitution cipher works by replacing each character in a message with another character. In this program, a random key is generated by shuffling a list of characters. That key is then used to convert the original message into an encrypted message and back again.
This project is useful for learning:
Basic Python programming
Lists and indexing
Loops
Randomization
Simple cryptography concepts
How the Program Works
- Import Libraries
The program imports two built-in Python libraries:
random → used to shuffle characters to create a random key.
string → provides sets of characters such as letters, digits, punctuation, and whitespace.
- Create Character List
The program creates a list containing many possible characters:
whitespace
punctuation
digits
uppercase letters
lowercase letters
chars=" "+ string.whitespace+string.punctuation+string.digits+string.ascii_letters chars=list(chars) 3. Create the Encryption Key
A copy of the character list is made and shuffled randomly.
key = chars.copy() random.shuffle(key)
This shuffled list becomes the encryption key.
Encryption Process
The user enters a message.
The program finds the position of each character in the chars list.
It replaces that character with the character at the same position in the key list.
Example:
plain_text=input("Enter a message to encrypt:") cipher_text=""
The encrypted message is then printed.
Decryption Process
The user enters the encrypted message.
The program finds each character inside the key list.
It converts it back to the original character from chars.
Example:
cipher_text=input("Enter a message to encrypt:") plain_text=""
The original message is reconstructed and printed.
Example Output Enter a message to encrypt: Hello123
Original message: Hello123 Encrypted message: !dT&7aP$
Decryption:
Enter a message to encrypt: !dT&7aP$
Encrypted message: !dT&7aP$ Original message: Hello123