-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencryptionPython.py
More file actions
89 lines (71 loc) · 2.69 KB
/
Copy pathencryptionPython.py
File metadata and controls
89 lines (71 loc) · 2.69 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
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import os, random, sys, pkg_resources
def encrypt(key, filename):
chunksize = 64 * 1024
outFile = os.path.join(os.path.dirname(filename), "(encrypted)" + os.path.basename(filename))
filsesize = str(os.path.getsize(filename)).zfill(16)
IV = ''
for i in range(16):
IV += chr(random.randint(0, 0xFF))
encryptor = AES.new(key, AES.MODE_CBC, IV)
with open(filename, "rb") as infile:
with open(outFile, "wb") as outFile:
outfile.write(filsesize)
outFile.write(IV)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) == 0:
chunk += ' ' * (16 - (len(chunk % 16)))
outFile.write(encryptor.encrypt(chunk))
def decrypt(ket, filename):
outFile = os.path.join(os.path.dirname(filename), os.path.basename(filename[11:]))
chunksize = 64 * 1024
with open(filename, "rb") as infile:
filesize = infile.read(16)
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
with open(outFile, "wb") as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(int(filesize))
def allfiles():
allFiles = []
for root, subfiles, files in os.walk(os.getcwd()):
for names in files:
allfiles.append(os.path.join(root, names))
return allFiles
choice = raw_input("Você gostaria de (E)ncriptar ou (D)ecriptar")
password = raw_input("Digite a senha: ")
encFiles = allfiles()
if choice == "E":
for Tfiles in encFiles:
if os.path.basename(Tfiles).startswith("(encrypted)"):
print ("%s is already encrypted" % (str(Tfiles)))
pass
elif Tfiles == os.path.join(os.getcwd(), sys.argv[0]):
pass
else:
encrypt(SHA256.new(password).digest(), str(Tfiles))
print ("Done encrypting %s" % str(Tfiles))
os.remove(Tfiles)
elif choice == "D":
filename = raw_input("Enter the filename to decrypt: ")
if not os.path.exists(filename):
print ("The file does not exist")
sys.exit(0)
elif not filename.startswith("(encrypted)"):
print ("%s is already not encrypted" % filename)
sys.exit()
else:
decrypt(SHA256.new(password).digest(), filename)
print ("Done decrypting %s" % filename)
os.remove
else:
print ("Digita algo certo pls.")
sys.exit()