-
Notifications
You must be signed in to change notification settings - Fork 5
/
ghostlib_win32crypto.py
58 lines (53 loc) · 1.94 KB
/
ghostlib_win32crypto.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
############################################################################
#
# © 2022 ABDULKADİR GÜNGÖR All Rights Reserved
# Contact email address: abdulkadir_gungor@outlook.com
#
# Developper: Abdulkadir GÜNGÖR (abdulkadir_gungor@outlook.com)
# Date: 04/2022
# All Rights Reserved (Tüm Hakları Saklıdır)
#
############################################################################
from Crypto.Cipher import AES
import base64
class Win32Crypto:
#
# key = 32 bytes , 0<number(int)<128
def __init__(self, key: bytes, number: int):
self.key = key
self.number = number
#
#
@staticmethod
def __byte_xor(data: bytes, number: int) -> bytes:
return bytes([b ^ number for b in data])
#
#
def encrypt(self, data):
try:
cipher = AES.new(self.key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
base64_nonce = (base64.b64encode((cipher.nonce)).replace(b'=', b'')) # 22 byte(s)
base64_tag = (base64.b64encode((tag)).replace(b'=', b'')) # 22 byte(s)
data = base64_nonce + base64_tag + ciphertext
return True, Win32Crypto.__byte_xor(data, self.number)
except:
return False, b''
#
#
def decrypt(self, data):
if len(data) > 43:
try:
data = Win32Crypto.__byte_xor(data, self.number)
base64_nonce = data[0:22] + b'=='
base64_tag = data[22:44] + b'=='
ciphertext = data[44:]
tag = base64.b64decode(base64_tag)
nonce = base64.b64decode(base64_nonce)
cipher = AES.new(self.key, AES.MODE_EAX, nonce=nonce)
res = cipher.decrypt_and_verify(ciphertext, tag)
return True, res
except:
return False, b''
else:
return False, b''