-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptionDES.py
81 lines (69 loc) · 2.65 KB
/
encryptionDES.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
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
import ARappServer.DBinterface as DBi
BLOCK_SIZE = 16
DES_KEY_FILE = 'U:\ВКР\ARappServer\Key.txt'
TEST_LOGIN = "TEST"
TEST_PASSWORD = "TEST0912375981237059812730"
# Обработка ошибки отсутствия файла с DES KEY
class FileEmpty(Exception):
def __init__(self, arg):
self.message = f"There is not DSA-encryption key in the file '{arg}'"
def __str__(self):
if self.message:
return 'FileEmpty, {0} '.format(self.message)
else:
return 'FileEmpty has been raised'
# Обработка ошибки неправильного ключа DES KEY
class WrongDES_Key(Exception):
def __init__(self, arg):
self.message = f"There is changed DSA-encryption key in the file '{arg}'" \
"\nChange DSA-encryption key or rewrite all users (including the base user) in database."
def __str__(self):
if self.message:
return 'WrongDES_Key, {0} '.format(self.message)
else:
return 'WrongDES_Key has been raised'
try:
file = open(DES_KEY_FILE, "rb")
if not open(DES_KEY_FILE, "rb").read():
raise FileEmpty(DES_KEY_FILE)
key = (file.read())
file.close()
except FileNotFoundError:
file = open(DES_KEY_FILE, "wb")
print(f"No such file: '{DES_KEY_FILE}'")
raise FileEmpty(DES_KEY_FILE)
##########################################################
def checkDES_Key():
'''
Проверка на наличие ключа DES KEY
:return:
'''
if "Incorrect login or password" == DBi.User_DB.getNamefromlogin(TEST_LOGIN, TEST_PASSWORD):
raise WrongDES_Key(DES_KEY_FILE)
def coding(password):
'''
Шифрование пароля, для хранения в базе данных
Шифрование производится алгоритмом DES
RFC 4772 (https://www.rfc-editor.org/rfc/rfc4772.txt)
:param password: <class 'str'> пароль !!! в незашифрованном виде
:return: <class 'bytes'> зашифрованный пароль
'''
des = DES.new(key, DES.MODE_ECB)
password_encoded = password.encode('utf8')
padded_password = pad(password_encoded, BLOCK_SIZE)
encrypted_password = des.encrypt(padded_password)
return encrypted_password
# # дешифровка
# def decoding(password):
# '''
# Дештифрование пароля
# :param name: password
# :return:
# '''
# des = DES.new(key, DES.MODE_ECB)
# data = des.decrypt(password)
# data = data.decode('utf-8')
# print("data", data)
# return data