-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonEncrypt&DecryptPhraseWithAKey.py
65 lines (54 loc) · 2.1 KB
/
PythonEncrypt&DecryptPhraseWithAKey.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
# Encrypt and decrypt a message using a Caesar cipher with a key number!
def main():
# Get the message to encrypt
message = input("Enter the message to encrypt: ")
# Get the key to use for the encryption
key = int(input("Enter the key to use for the encryption: "))
# Encrypt the message
ciphertext = encrypt(message, key)
# Print the encrypted message
print("The encrypted message is: " + ciphertext)
# Decrypt the message
plaintext = decrypt(ciphertext, key)
# Print the decrypted message
print("The decrypted message is: " + plaintext)
def encrypt(message, key):
# Convert the message to uppercase
message = message.upper()
# Create an empty string to store the encrypted message
ciphertext = ""
# Loop through each character in the message
for c in message:
# Convert the character to ASCII
ascii = ord(c)
# If the character is a letter
if ascii >= 65 and ascii <= 90:
# Add the encrypted character to the encrypted message
ciphertext += chr((ascii - 65 + key) % 26 + 65)
# If the character is not a letter
else:
# Add the character to the encrypted message
ciphertext += c
# Return the encrypted message
return ciphertext
def decrypt(ciphertext, key):
# Convert the ciphertext to uppercase
ciphertext = ciphertext.upper()
# Create an empty string to store the decrypted message
plaintext = ""
# Loop through each character in the ciphertext
for c in ciphertext:
# Convert the character to ASCII
ascii = ord(c)
# If the character is a letter
if ascii >= 65 and ascii <= 90:
# Add the decrypted character to the decrypted message
plaintext += chr((ascii - 65 - key) % 26 + 65)
# If the character is not a letter
else:
# Add the character to the decrypted message
plaintext += c
# Return the decrypted message
return plaintext
if __name__ == "__main__":
main()