-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-encrypt-1.py
138 lines (114 loc) · 3.59 KB
/
file-encrypt-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
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
# --------------------------------------------------------
#
# File Read and Encrypt Demo
#
# Folder: session-07
# Filename: file-encrypt-1.py
# Author: Adrian Gould <[email protected]>
# Version: 0.0
#
# --------------------------------------------------------
def welcome():
'''
Welcome message
'''
print("Welcome to the Caesar Cypher program")
name = input("Enter your name: ")
print(name, " - hope you enjoy encrypting and decrypting")
return name
def get_input_filename():
'''
Obtain the filename of the file to be encrypted/decrypted
'''
while True:
print()
print("#" * 64)
print("# Please enter the filename to encrypt/decrypt #")
print("# Enter Q to quit #")
print("#" * 64)
filename = input("Enter filename:")
if "Q" == filename.upper():
quit()
try:
file_handle = open(filename, "r")
return file_handle
except:
print("The file does not exist... Please try again.")
def get_output_filename():
'''
Obtain the filename to write the encrypted/decrypted data to
'''
while True:
print()
print("#" * 64)
print("# Please enter the filename write to #")
print("# or enter Q to quit #")
print("#" * 64)
filename = input("Enter filename:")
if "Q" == filename.upper():
print("Terminating program...")
quit()
if "" == filename:
print("No filename given. Please try again.")
else:
output_file_handle = open(filename, "w")
return output_file_handle, filename
def validate_input_file(prefix):
'''
Check if the file's first line contains encrypt/decrypt indicator
- line structure: identifier shift_value
- identifier: e = encrypt, d= decrypt
- shift_value: integer value
'''
prefix = prefix.lower()
try:
shift = int(prefix[1:].strip())
except ValueError:
message = f"Shift {prefix[1:]} is not a number. Unable to encrypt/decrypt"
else:
if prefix[0] in 'ed':
indicator = prefix[0]
return indicator, shift
else:
message = "Cannot Encrypt/Decrypt data due to invalid input"
print(message)
print("Program terminating...")
quit()
def encrypt(shift, line):
'''
Encrypt the message line
'''
message = ""
for letter in line:
new_character = ord(letter) + shift
message = message + chr(new_character)
return message
def decrypt(shift, line):
'''
Decrypt the line of data
'''
return encrypt(-shift, line)
def main():
first_line = True
name = welcome()
encrypt_file_handle = get_input_filename()
for line in encrypt_file_handle:
if first_line is True:
first_line = False
encrypt_indicator, shift = validate_input_file(line)
output_file, output_filename = get_output_filename()
if encrypt_indicator == "e":
output_file.write(f"d {shift}\n")
elif encrypt_indicator == "d":
output_file.write(f"e {shift}\n")
else:
line = line.strip()
if encrypt_indicator == "e":
message = encrypt(shift, line)
else:
message = decrypt(shift, line)
output_file.write(f"{message}\n")
output_file.close()
print()
print(f"{name}, your file: {output_filename} was successfully created.")
main()