Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epsylon
GitHub Repository: epsylon/ufonet
Path: blob/master/core/tools/crypter.py
1205 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-"
3
"""
4
This file is part of the UFONet project, https://ufonet.03c8.net
5
6
Copyright (c) 2013/2024 | psy <[email protected]>
7
8
You should have received a copy of the GNU General Public License along
9
with UFONet; if not, write to the Free Software Foundation, Inc., 51
10
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
11
"""
12
###################################################################
13
# Code extracted from project: AnonTwi (anontwi.03c8.net)
14
###################################################################
15
16
KEY_SIZE = 32
17
BLOCK_SIZE = 16
18
MAC_SIZE = 20
19
20
import base64
21
from os import urandom
22
from hashlib import sha1, sha256
23
from Cryptodome.Cipher import AES
24
25
trans_5C = ''.join([chr (x ^ 0x5c) for x in range(256)])
26
trans_36 = ''.join([chr (x ^ 0x36) for x in range(256)])
27
trans_5C = trans_5C.encode("latin-1")
28
trans_36 = trans_36.encode("latin-1")
29
30
def hmac_sha1(key, msg):
31
if len(key) > 20:
32
key = sha1(key).digest()
33
key += chr(0).encode('utf-8') * (20 - len(key))
34
o_key_pad = key.translate(trans_5C)
35
i_key_pad = key.translate(trans_36)
36
return sha1(o_key_pad + sha1(i_key_pad + msg).digest()).digest()
37
38
def derive_keys(key):
39
h = sha256()
40
h.update(key)
41
h.update('cipher'.encode('utf-8'))
42
cipher_key = h.digest()
43
h = sha256()
44
h.update(key)
45
h.update('mac'.encode('utf-8'))
46
mac_key = h.digest()
47
return (cipher_key, mac_key)
48
49
def generate_key():
50
return base64.b64encode(urandom(KEY_SIZE))
51
52
class Cipher(object):
53
def __init__(self, key="", text=""):
54
self.block_size = 16
55
self.mac_size = 20
56
self.key = self.set_key(key)
57
self.text = self.set_text(text)
58
self.mode = AES.MODE_CFB
59
60
def set_key(self, key):
61
try:
62
key = base64.b64decode(key)
63
except TypeError:
64
raise ValueError
65
self.key = key
66
return self.key
67
68
def set_text(self, text):
69
self.text = text.encode("utf-8")
70
return self.text
71
72
def encrypt(self):
73
if BLOCK_SIZE + len(self.text) + MAC_SIZE > 105:
74
self.text = self.text[:105 - BLOCK_SIZE - MAC_SIZE]
75
(cipher_key, mac_key) = derive_keys(self.key)
76
iv = urandom(BLOCK_SIZE)
77
aes = AES.new(cipher_key, self.mode, iv)
78
ciphertext = aes.encrypt(self.text)
79
mac = hmac_sha1(mac_key, iv + ciphertext)
80
return base64.b64encode(iv + ciphertext + mac)
81
82
def decrypt(self):
83
try:
84
iv_ciphertext_mac = base64.urlsafe_b64decode(self.text)
85
except:
86
try:
87
padding = len(self.text) % 4
88
if padding == 1:
89
return ''
90
elif padding == 2:
91
self.text += b'=='
92
elif padding == 3:
93
self.text += b'='
94
iv_ciphertext_mac = base64.urlsafe_b64decode(self.text)
95
except TypeError:
96
return None
97
iv = iv_ciphertext_mac[:BLOCK_SIZE]
98
ciphertext = iv_ciphertext_mac[BLOCK_SIZE:-MAC_SIZE]
99
mac = iv_ciphertext_mac[-MAC_SIZE:]
100
(cipher_key, mac_key) = derive_keys(self.key)
101
expected_mac = hmac_sha1(mac_key, iv + ciphertext)
102
if mac != expected_mac:
103
return None
104
aes = AES.new(cipher_key, self.mode, iv)
105
return aes.decrypt(ciphertext)
106
107
if __name__ == "__main__":
108
print("\nUFONet Crypter (AES256+HMAC-SHA1) -> (140 plain text chars = 69 encrypted chars)\n")
109
text = str(input("-> Enter TEXT: "))
110
input_key = str(input("\n-> Enter KEY: "))
111
key = base64.b64encode(input_key.encode('utf-8'))
112
c = Cipher(key, text)
113
msg = c.encrypt()
114
msg = msg.decode('utf-8')
115
c.set_text(msg)
116
print("\n" + " " + '-'*44)
117
print('\n-> Ciphertext: [', msg, ']')
118
print('\n-> Length:', len(msg))
119
print("\n" + " " + '-'*44)
120
print('\n-> Key (share it using SNEAKNET!):', input_key)
121
print('\n-> Decryption PoC:', c.decrypt().decode('utf-8'), "\n")
122
123