-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_encryption_generator.py
More file actions
29 lines (22 loc) · 965 Bytes
/
Copy pathtext_encryption_generator.py
File metadata and controls
29 lines (22 loc) · 965 Bytes
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
# Text Encrpytion Generator
import random
alphabets = "abcdefghijklmnopqrstuvwxyz"
shift = random.randint(5, 10)
encrypted_text = ""
try:
text = input("Enter text (only letters, no spaces or special characters): ")
if not text.isalpha():
raise ValueError("Text must contain only letters (a-z or A-Z), no numbers or special characters.")
for ch in text:
is_upper = ch.isupper()
ch = ch.lower()
index = alphabets.find(ch)
new_index = (index + shift) % len(alphabets) # this logic works better. Remember, whenever you want to startover anything used "%(modulo operator)".
encrypted_char = alphabets[new_index]
if is_upper:
encrypted_char = encrypted_char.upper()
encrypted_text += encrypted_char
print(f"Encrypted text: {encrypted_text}")
print(f"Encryption shift key: {shift}")
except ValueError as e:
print(f"ERROR! {e}")