-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha09_caesar_cipher.py
More file actions
144 lines (125 loc) · 6.99 KB
/
a09_caesar_cipher.py
File metadata and controls
144 lines (125 loc) · 6.99 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
######################################################################
# Author: Emily Lovell & Scott Heggen TODO: Elaheh Jamali & Emely Alfaro Zavala
# Username: lovelle & heggens TODO: Jamalie & Alfarozavalae
#
# Assignment: A09: Caesar Cipher
#
# Purpose: The class imports a file, encrypts the file, and exports the cipher to a new file
# You need to implement the decrypt function.
######################################################################
# Acknowledgements:
#
# Acknowledgements: The original code was created by Dr. Scott Heggen
# and modified by Dr. Jan Pearce
#
# licensed under a Creative Commons
# Attribution-Noncommercial-Share Alike 3.0 United States License.
####################################################################################
class CaesarCipher:
"""
The CaesarCipher class represents and manipulates key, input file and the crypt type
This class will import a file that needs to be encrypted or decrypted, with an integer representing the key
it will decrypt or decrypt the given file
"""
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # The alphabet, which will be used to do our shifts
def __init__(self, input_file = "message_input.txt", key = 0, crypt_type = "encrypt"):
"""
A constructor for the CaesarCipher class
:param input_file: The file to be encrypted/decrypted
:param key: The amount each message/cipher needs shifted
:param crypt_type: Either encrypt or decrypt
"""
self.input_file = input_file # The file to be encrypted or decrypted
self.key = key # The amount each message/cipher will be shifted
self.message = "" # A placeholder for the message
self.cipher = "" # A placeholder for the cipher
self.crypt_type = crypt_type # Either "encrypt" or "decrypt"
self.import_file() # Calls the import_file() method below
def import_file(self):
"""
Imports a file stored in the variable self.input_file
:return: a string representing the contents of the file
"""
f = open(self.input_file, "r")
if self.crypt_type == "encrypt":
self.message = f.read() # Set self.message to the file contents
elif self.crypt_type == "decrypt":
self.cipher = f.read() # Set self.cipher to the file contents
f.close()
if __name__ == "__main__":
print("File imported: {0}".format(self.input_file))
def export_file(self, text_to_export, filename):
"""
Exports a file called filename
:param text_to_export: the string to be written to the exported file
:param filename: a string representing the name of the file to be exported to
"""
f = open(filename, "w")
f.write(text_to_export)
f.close()
if __name__ == "__main__":
print("File exported: {0}".format(filename))
def encrypt(self):
"""
Converts an original message into a ciphered message with each letter shifted to the right by the key
:return: a string representing the ciphertext.txt
"""
output = ""
for i in self.message:
if i.upper() in self.alphabet:
old_letter = self.alphabet.find(i.upper())
# Uses modulus to return the correct index for each letter after the shift
# (for cases where the index is outside the range of self.alphabet,
# it wraps back to the beginning of the alphabet)
output += self.alphabet[(old_letter + self.key) % 26]
else:
output += i # Adds non-alphabet characters directly
if __name__ == "__main__":
print("Message Encrypted")
return output
def decrypt(self):
"""
Converts a ciphertext.txt into an original message by shifting each letter to the left by the key
:return: a string representing the original message
"""
# TODO Complete the decrypt method
output = ""
for i in self.cipher:
if i.upper() in self.alphabet:
new_letter = self.alphabet.find(i.upper())
# we use the module to return the correct module after the inverse
# when the index is negative it will go back to the end of the alphabet
output += self.alphabet[new_letter - self.key % 26]
else:
output += i # Adds non-alphabet characters directly
if __name__ == "__main__":
print("Message Decrypted")
return output # Obviously this should output something else
def main():
# A sample encryption
cipher0 = CaesarCipher("message_input.txt", 2, "encrypt") # Constructs a new CaesarCipher object called cipher0
cipher_text0 = cipher0.encrypt() # Encrypts the file specified in the constructor
cipher0.export_file(cipher_text0, "cipher_sample.txt") # Writes the output to a file
# Caesar has some letters to send and receive.
# Letter 1 goes to P. Lentulus Spinther, who has agreed with Caesar to use a key of 3
# TODO Construct a new CaesarCipher object called cipher_lentulus
cipher_lentulus = CaesarCipher("letter_to_friend_1.txt", 3, "encrypt") # constructing a new CaesarCipher object
# TODO Encrypt the file specified in the constructor
cipher_lentulus_text = cipher_lentulus.encrypt() # encrypts the file specified in the constructor
# TODO Write the output to a file
cipher_lentulus.export_file(cipher_lentulus_text, "cipher_to_friend_1.txt") # writes the output to a file
# Letter 2 goes to Marcus Tullius Cicero, who has agreed to use a key of 14
# TODO Construct a new CaesarCipher object called cipher_marcus
cipher_marcus = CaesarCipher("letter_to_friend_2.txt", 14, "encrypt") # constructing a new CaesarCipher object
# TODO Encrypt the file specified in the constructor
cipher_marcus_text = cipher_marcus.encrypt() # encrypts the file specified in the constructor
# TODO Write the output to a file
cipher_marcus.export_file(cipher_marcus_text, "cipher_to_friend_2.txt") # writes the output to a file
# Letter 3 is coming from Cicero for Caesar to decrypt. Again, they agreed to use key 14
cipher3 = CaesarCipher("cipher_from_friend_3.txt", 14, "decrypt") # constructs a new CaesarCipher object called cipher3
# TODO Decrypt the file specified in the constructor
cipher3_text = cipher3.decrypt() # decrypts the file specified in the constructor
# TODO Write the output to a file using the export_file() method
cipher3.export_file(cipher3_text, "message_from_friend_3.txt") # writes the output to a file
if __name__ == "__main__":
main()