Repository navigation
Expand file tree
/
Copy pathCryptHome.py
More file actions
87 lines (77 loc) · 2.79 KB
/
Copy pathCryptHome.py
File metadata and controls
87 lines (77 loc) · 2.79 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
# encoding: utf-8
# encryptage et decryptage des fichier image
from datetime import datetime
from Crypto.Cipher import AES
from Crypto.Protocol import KDF
from io import BytesIO
from AppVar import Global
class CryptHome(object):
__key = b"this is the secret key that doesn't be know by anyone!"
@staticmethod
def encryptFile(InfileName, filename=False):
#print("_____ procedure de cryptage_____")
dKey = CryptHome.__keyGen(CryptHome.__key)
OutfileName, Ifile, Ofile = "", None, None
lineFile = b""
cipher = AES.new(dKey, AES.MODE_CFB)
if not filename:
OutfileName = Global.dataDirPath + "\\" + CryptHome.__fileNameGen()
else:
OutfileName = filename
try:
Ifile = open(InfileName, "rb")
for line in Ifile.readlines():
lineFile += line
except IOError:
print("error Infile")
return False
finally:
Ifile.close()
ClineFile = cipher.iv + cipher.encrypt(lineFile)
try:
Ofile = open(OutfileName, "wb")
Ofile.write(ClineFile)
except IOError:
print("error Outfile")
return False
finally:
Ofile.close()
return OutfileName
@staticmethod
def decryptFile(fileName):
#print("_____ procedure de decryptage_____")
dKey = CryptHome.__keyGen(CryptHome.__key)
lineFile, Ifile = b"", None
cipher = AES.new(dKey, AES.MODE_CFB)
try:
Ifile = open(fileName, "rb")
for line in Ifile.readlines():
lineFile += line
except IOError:
return False
finally:
Ifile.close()
return BytesIO(cipher.decrypt(lineFile)[16:])
@staticmethod
def __keyGen(passPhrase):
key_size = 32
iteration = 2206
salt = b"this could be random but i don't believe in chance"
dKey = KDF.PBKDF2(passPhrase, salt, key_size, iteration)
return dKey
@staticmethod
def __fileNameGen():
"""systeme de nommage de fichier qui retourne un nom formaté par date année/mois/jour/heure/minute/seconde"""
filename = str(datetime.today().year) + str(datetime.today().month) + str(datetime.today().day) + str(
datetime.today().hour) + str(datetime.today().minute) + str(datetime.today().second) + ".bin"
return filename
if __name__ == "__main__":
rep = input("Would you Encrypt or Decrypt ? ")
fName = input("enter the file name with the extension : ")
if rep in ["encrypt", "e", "E"]:
CryptHome.encryptFile(fName)
else:
file = open("contents\\image.png", "wb")
data = CryptHome.decryptFile(fName)
file.writelines(data.readlines())
print("Ok")