-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cryptography_1.py
60 lines (51 loc) · 1.85 KB
/
Cryptography_1.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
message = input("Please enter the secret message: ")
key = input("Please enter a key: ")
while (type(key)!= int) and int(key) < 0:
print("Please enter a positive integer value for key!")
key = int(input("Please enter a key: "))
instruction = input("Decode or Encode? ")
while instruction.lower() != "decode" and instruction.lower() != "encode":
print("Please enter either 'Decode' or 'Encode'")
instruction = input("Decode or Encode: ")
#Encryption
if instruction == "Encode" or instruction == "encode":
encode = ""
for character in message:
if character.isalpha():
key = int(key)
new_ord = ord(character) + key
if new_ord > ord('z') and character.islower():
new_ord -= 26
new_chr = chr(new_ord)
encode += new_chr
elif new_ord > ord('Z') and character.isupper():
new_ord -= 26
new_chr = chr(new_ord)
encode += new_chr
else:
new_chr = chr(new_ord)
encode += new_chr
else:
encode += character
print("Encryption: ", encode)
#Decryption
elif instruction == "Decode" or instruction == "decode":
decode = ""
for character in message:
if character.isalpha():
key = int(key)
new_ord = ord(character) - key
if new_ord < ord('a') and character.islower():
new_ord += 26
new_chr = chr(new_ord)
decode += new_chr
elif new_ord < ord('A') and character.isupper():
new_ord += 26
new_chr = chr(new_ord)
decode += new_chr
else:
new_chr = chr(new_ord)
decode += new_chr
else:
decode += character
print("Decryption: ", decode)