-
Notifications
You must be signed in to change notification settings - Fork 0
/
symmetric-alg.py
68 lines (53 loc) · 1.52 KB
/
symmetric-alg.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
def save_file(results, file_path):
f = open(file_path, 'w')
f.write(results)
f.close()
def open_file(file_path):
f = open(file_path, 'r')
file_contents = f.read()
f.close()
return file_contents
def calculate_key(key):
results = 0
counter = 0
for char in key:
counter += 1
results += ord(char)
return int(results / counter)
def decrypt(file_path, key):
file_contents = open_file(file_path)
key_calc = calculate_key(key)
dec_results = ''
for line in file_contents:
for wrd in line:
for char in wrd:
int_char = ord (char) - key_calc
dec_results += chr (int_char)
save_file(dec_results, file_path)
print '[!] Finished Decryption'
def encrypt(file_path, key):
file_contents = open_file(file_path)
key_calc = calculate_key(key)
enc_results = ''
for line in file_contents:
for wrd in line:
for char in wrd:
int_char = ord(char) + key_calc
enc_results += chr(int_char)
save_file(enc_results, file_path)
print '[!] Finished Encryption'
def main():
print '[1] Encrypt \n[2] Decrypt'
choice = raw_input('>')
print 'File Path: '
file_path = raw_input ('>')
print 'Secret Key: '
key = raw_input('>')
if choice == "1": # Encrypt
encrypt(file_path, key)
elif choice == "2": # Decrypt
decrypt(file_path, key)
else:
print 'Invalid Choice'
if __name__ == '__main__':
main ()