-
Notifications
You must be signed in to change notification settings - Fork 29
/
Şifre.py
85 lines (63 loc) · 1.97 KB
/
Şifre.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
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
"""Crypto: tool for encrypting and decrypting messages.
Exercises
1. Review 'ord' and 'chr' functions and letter-to-number mapping.
2. Explain what happens if you use key 26.
3. Find a way to decode a message without a key.
4. Encrypt numbers.
5. Make the encryption harder to decode.
Adapted from code in https://inventwithpython.com/chapter14.html
"""
def encrypt(message, key):
"Encrypt message with key."
result = ''
# Iterate letters in message and encrypt each individually.
for letter in message:
if letter.isalpha():
# Letters are numbered like so:
# A, B, C - Z is 65, 66, 67 - 90
# a, b, c - z is 97, 98, 99 - 122
num = ord(letter)
if letter.isupper():
base = ord('A')
elif letter.islower():
base = ord('a')
# The encryption equation:
num = (num - base + key) % 26 + base
result += chr(num)
elif letter.isdigit():
# TODO: Encrypt digits.
result += letter
else:
result += letter
return result
def decrypt(message, key):
"Decrypt message with key."
return encrypt(message, -key)
def decode(message):
"Decode message without key."
pass # TODO
def get_key():
"Get key from user."
try:
text = input('Enter a key (1 - 25): ')
key = int(text)
return key
except:
print('Invalid key. Using key: 0.')
return 0
print('Do you wish to encrypt, decrypt, or decode a message?')
choice = input()
if choice == 'encrypt':
phrase = input('Message: ')
code = get_key()
print('Encrypted message:', encrypt(phrase, code))
elif choice == 'decrypt':
phrase = input('Message: ')
code = get_key()
print('Decrypted message:', decrypt(phrase, code))
elif choice == 'decode':
phrase = input('Message: ')
print('Decoding message:')
decode(phrase)
else:
print('Error: Unrecognized Command')