-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (94 loc) · 3.51 KB
/
main.py
File metadata and controls
107 lines (94 loc) · 3.51 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# ============================================
# CHAPTER 44: ENCRYPTION PROGRAM
# ============================================
# Substitution Cipher Encryption and Decryption
#
# User Story:
# As a user, I want to encrypt and decrypt messages using a substitution cipher
# so that I can securely communicate without messages being easily read by
# unintended recipients.
#
# How Substitution Cipher Works:
# 1. Create list of all possible characters (alphabet)
# 2. Create shuffled "key" - random arrangement of same characters
# 3. Encryption: Replace each character with its corresponding key character
# 4. Decryption: Reverse the process using the key
#
# Example:
# chars: a b c d e f g h...
# key: q w e r t y u i...
# "bad" encrypts to "wqr"
#
# Key Concepts:
# - String module for character sets
# - List manipulation and copying
# - Random shuffling
# - Character substitution algorithm
# - Index-based mapping
import random
import string
# =============================================
# BUILD CHARACTER SET
# =============================================
# Combine all possible characters:
# - Space character
# - Punctuation (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)
# - Digits (0-9)
# - ASCII letters (a-z, A-Z)
chars = "" + " " + string.punctuation + string.digits + string.ascii_letters
# Convert string to list for manipulation
# Lists are mutable, strings are not
chars = list(chars)
# =============================================
# CREATE ENCRYPTION KEY
# =============================================
# Make a copy of chars list (not a reference)
# copy() creates independent list
key = chars.copy()
# Randomly shuffle the key
# This creates the substitution cipher mapping
random.shuffle(key)
# =============================================
# DISPLAY CHARACTER MAPPINGS (For debugging)
# =============================================
# Show original characters and their encrypted equivalents
print(f"chars: {chars}")
print(f"key: {key}")
# =============================================
# ENCRYPTION PROCESS
# =============================================
# Get message from user
plain_text = input("Enter a message to encrypt: ")
# Initialize empty encrypted message
cipher_text = ""
# =============================================
# ENCRYPT EACH CHARACTER
# =============================================
# For each letter in the original message:
for letter in plain_text:
# Find position of letter in original character set
# Example: 'a' might be at index 37 in chars
index_of_letter = chars.index(letter)
# Replace with character at same position in key
# Example: chars[37] = 'a', key[37] might be 'q'
# So 'a' gets encrypted to 'q'
cipher_text += key[index_of_letter]
# =============================================
# DISPLAY RESULTS
# =============================================
print(f'original message: {plain_text}')
print(f'encrypted message: {cipher_text}')
# =============================================
# HOW TO DECRYPT (not implemented here)
# =============================================
# To decrypt, reverse the process:
# for letter in cipher_text:
# index_of_letter = key.index(letter) # Find in key
# decrypted_text += chars[index_of_letter] # Use chars at same index
# =============================================
# IMPORTANT NOTES
# =============================================
# 1. Both sender and receiver need the SAME KEY
# 2. Key must be kept secret (not shown to others)
# 3. This is a simple cipher - not cryptographically secure
# 4. For real security, use proven encryption libraries