-
Notifications
You must be signed in to change notification settings - Fork 1
/
henpei_crypto.py
141 lines (100 loc) · 3.8 KB
/
henpei_crypto.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP
import hashlib
import zlib
import base64
# Courtesy of https://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256/40687785
class AESCipher(object):
def __init__(self, key):
self.bs = 32
self.key = hashlib.sha256(key.encode()).digest()
def encrypt(self, raw):
raw = self._pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
def decrypt(self, enc):
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
class RSACipher:
def __init__(self, pair):
self.pair = pair
try:
self.pair['private'] = str.encode(self.pair['private'])
self.pair['public'] = str.encode(self.pair['public'])
except:
pass
# OKOK WE SO WANT TO STORE THE KEYS AS BYTES OKOK
if b'PRIVATE' in pair['private']:
self.private = RSA.importKey(pair['private'])
else:
self.private = None
self.public = RSA.importKey(pair['public'])
def encrypt(self, object):
cipher = PKCS1_OAEP.new(self.public)
return cipher.encrypt(object)
def decrypt(self, object):
cipher = PKCS1_OAEP.new(self.private)
return cipher.decrypt(object)
def cube_encrypt(self, object):
return RSACipher(self.pair).encrypt(str.encode(object)).hex()
class Cube:
def __init__(self, sequence):
self.key = hashlib.sha512(str.encode(str(sequence))).hexdigest() # Get a hash of the cube config
self.rsa_cipher = None
self.aes_cipher = AESCipher(self.key)
self.pair = { 'public' : None, 'private' : None }
def import_pair(self, pair):
self.pair = pair
self.pair['public'] = str.encode(self.pair['public'])
self.pair['private'] = str.encode(self.aes_decrypt(pair['private']))
self.rsa_cipher = RSACipher(self.pair)
def export_pair(self):
try:
self.pair['public'] = self.pair['public'].decode()
except:
pass
try:
self.pair['private'] = self.pair['private'].decode()
except:
pass
public = self.pair['public']
private = self.aes_encrypt(self.pair['private'])
export = { 'public' : public, 'private' : private.decode() }
return export
def generate_pair(self):
new_key = RSA.generate(4096, e=65537)
private_key = new_key.exportKey("PEM")
public_key = new_key.publickey().exportKey("PEM")
self.pair = {'public': public_key.decode(), 'private': private_key.decode()}
self.rsa_cipher = RSACipher(self.pair)
def aes_encrypt(self, object):
return self.aes_cipher.encrypt(object)
def aes_decrypt(self, object):
return self.aes_cipher.decrypt(object)
def encrypt(self, object):
return self.rsa_cipher.encrypt(object).hex()
def decrypt(self, object):
try:
return self.rsa_cipher.decrypt(bytes.fromhex(object)).decode()
except:
print('lmao u fucked up')
return ''
if __name__ == '__main__':
c = Cube([123,123,213])
c.generate_pair()
lol = c.encrypt(b"Hello, world!")
thing = c.export_pair()
#print(c.aes_decrypt(thing['private']))
m.import_pair(thing)
print(lol)
print(m.decrypt(lol))