This repository was archived by the owner on Jun 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcriptodoc.py
More file actions
57 lines (36 loc) · 1.23 KB
/
Copy pathcriptodoc.py
File metadata and controls
57 lines (36 loc) · 1.23 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
def replace(index, key, start, end):
n = end - start + 1
k = (index + key) % (end + 1) + ((index + key) // (end + 1)) * start
if index + key < start:
k = k + n
return chr(k)
def encrypt(message, key):
# 65-90 97-122
nA, nZ, na, nz = ord('A'), ord('Z'), ord('a'), ord('z')
encrypted = ''
for character in message:
index = ord(character)
new_word = character
if nA <= index <= nZ:
new_word = replace(index, key, nA, nZ)
elif index in range(na, nz + 1):
new_word = replace(index, key, na, nz)
encrypted = encrypted + new_word
return encrypted
def decrypt(message, key):
return encrypt(message, -key)
def encryptdoc(source, destination, key):
file = open(source, 'r')
content = file.read()
file.close()
encrypted = encrypt(content, key)
file = open(destination, "w")
file.write(encrypted)
file.close()
def decryptdoc(source, destination, key):
return encryptdoc(source, destination, -key)
key = 7
encryptdoc("message.txt", "message_encrypted.txt", key)
print("Message encrypted successfully!")
decryptdoc("message_encrypted.txt", "message_decrypted.txt", key)
print("Message decrypted successfully!")