Skip to content

Commit 5bd0b10

Browse files
committed
wiki119 | Encryption/enc.py | fixed encryption morse-code and added decryption code
1 parent 5502959 commit 5bd0b10

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

Encryption/enc.py

+68-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import base64
22
import binascii
33

4-
message = input("Please enter the text you would like to encrypt or decrypt: ")
4+
# ~~~~~~~~~~~~~~~~~~~~~~ Encryption ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
message = input("Please enter the text you would like to encrypt ")
56
option = input("Please select the method you'd like to use: base64/morse/hex/caesar cipher: ")
67
while option == 'base64' or option == 'morse' or option == 'hex' or option == 'caesar cipher':
78
if option == 'base64':
@@ -13,17 +14,20 @@
1314

1415
if option == 'morse':
1516
Morse_Dict = {'A':'.-', 'B':'-...','C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....','I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.','O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-','U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..','1':'.----', '2':'..---', '3':'...--','4':'....-', '5':'.....', '6':'-....','7':'--...', '8':'---..', '9':'----.','0':'-----', ', ':'--..--', '.':'.-.-.-','?':'..--..', '/':'-..-.', '-':'-....-','(':'-.--.', ')':'-.--.-'}
16-
def encryption(message):
17+
def encryption(message):
1718
cipher = ' '
1819
for letter in message:
1920
if letter != ' ':
2021
cipher += Morse_Dict[letter] + ' '
2122
else:
2223
cipher += ' '
2324
print(cipher)
24-
encryption(message)
25+
encryption(message)
26+
27+
2528
if option == 'hex':
2629
print(message.encode("utf-8").hex())
30+
2731
if option == 'caesar cipher':
2832
s = int(input("Please enter the shift number you'd like (Exmaple: 4): "))
2933
def encrypt(message,s):
@@ -42,3 +46,64 @@ def encrypt(message,s):
4246
encrypt(message, s)
4347
break
4448

49+
# ~~~~~~~~~~~~~~~~~~~~~~ DECRYPTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50+
message = input("Please enter the text you would like to decrypt ")
51+
option = input("Please select the method you'd like to use: base64/morse/hex/caesar cipher: ")
52+
while option == 'base64' or option == 'morse' or option == 'hex' or option == 'caesar cipher':
53+
if option == 'base64':
54+
message_bytes = message.encode('ascii')
55+
base64_bytes = base64.b64decode(message_bytes)
56+
base64_message = base64_bytes.decode('ascii')
57+
print("Encrypted Message:" + base64_message)
58+
break
59+
60+
61+
if option == 'morse':
62+
Morse_Dict = {'A':'.-', 'B':'-...','C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....','I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.','O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-','U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..','1':'.----', '2':'..---', '3':'...--','4':'....-', '5':'.....', '6':'-....','7':'--...', '8':'---..', '9':'----.','0':'-----', ', ':'--..--', '.':'.-.-.-','?':'..--..', '/':'-..-.', '-':'-....-','(':'-.--.', ')':'-.--.-'}
63+
def decrypt(message):
64+
var = '' #variable for concatenating letters
65+
decode = ''
66+
for letter in message:
67+
if (letter != ' '):
68+
c = 0 #counter for spaces to keep letters separated
69+
var += letter
70+
else:
71+
c += 1
72+
if c == 2:
73+
decode += ' '
74+
else:
75+
decode += list(Morse_Dict.keys())[list(Morse_Dict.values()).index(var)]
76+
var = ''
77+
return decode
78+
result = decrypt(message)
79+
print (result)
80+
break
81+
82+
83+
if option == 'hex':
84+
hex_bytes = bytes.fromhex(message)
85+
message_bytes = hex_bytes.decode('utf-8')
86+
result = message_bytes
87+
print(result)
88+
break
89+
90+
if option == 'caesar cipher':
91+
s = int(input("Please enter the shift number you'd like (Exmaple: 4): "))
92+
def decrypt2(message,s):
93+
result = ""
94+
# transverse the plain text
95+
for i in range(len(message)):
96+
char = message[i]
97+
# Encrypt uppercase characters in plain text
98+
99+
if (char.isupper()):
100+
result += chr((ord(char) - s-65) % 26 + 65)
101+
# Encrypt lowercase characters in plain text
102+
else:
103+
result += chr((ord(char) - s - 97) % 26 + 97)
104+
print(result)
105+
decrypt2(message, s)
106+
break
107+
108+
109+

0 commit comments

Comments
 (0)