-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhide.py
66 lines (52 loc) · 1.86 KB
/
hide.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
from Crypto.Cipher import AES
import time
from io import BytesIO
import tarfile
class Hide():
def __init__(self, key: str):
self.key = bytes(key, encoding="UTF-8")
self.aes = AES.new(self.pad(self.key), AES.MODE_ECB)
def pad(self, text: bytes) -> bytes:
while len(text) % 16 != 0:
text += b' '
return text
def encode(self, content) -> bytes:
return self.aes.encrypt(self.pad(content))
def decode(self, content) -> bytes:
return self.aes.decrypt(content)
def merge(self, fileName: str, content: bytes):
nowTime = time.localtime()
prefix = "out_" \
+ str(nowTime.tm_year) \
+ str(nowTime.tm_mon) \
+ str(nowTime.tm_mday) \
+ str(nowTime.tm_hour) \
+ str(nowTime.tm_min) \
+ str(nowTime.tm_sec) \
+ "_"
with open(fileName, 'rb') as fr:
frContent = fr.read()
with open('size.txt', 'w') as fs:
fs.write(str(len(frContent)))
with open(prefix + fileName, 'wb') as fo:
fo.write(frContent)
fo.write(content)
def splite(self, fileName: str, preLenth: int) -> bytes:
with open(fileName, 'rb') as fr:
_ = fr.read(preLenth)
frContent = fr.read()
return frContent
def package(self, fileName: str) -> bytes:
buffer = BytesIO()
tar = tarfile.open(fileobj=buffer, mode='w')
tar.add(fileName)
buffer.seek(0)
return buffer.read()
def unpackage(self,content:bytes):
buffer = BytesIO()
buffer.write(content)
buffer.seek(0)
tar = tarfile.open(fileobj=buffer,mode='r')
fileNames = tar.getnames()
for fileName in fileNames:
tar.extract(fileName, "./outdir")