-
Notifications
You must be signed in to change notification settings - Fork 73
/
crypto.py
155 lines (119 loc) · 3.89 KB
/
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python2
import os, argparse
from getpass import getpass
# sudo pip install pycryptodome==3.6.1
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random
green = "\033[32m"
blue = "\033[34m"
red = "\033[31m"
bold = "\033[1m"
end = "\033[0m"
print(red+"""
|
|
----+---- ---------
|
|
) (
\ \ / /
\ |\ / |/
\| \ hack1lab / /
\ |\ -------- / | /
\ | \_______________________/ | /
\ | | | | | |/
\| | | | | /
\____|______|______|______|___/
Files Encryption
fb.me/hack1lab, @hack1lab
"""+end)
def encrypt(key, filename, ig):
chunksize = 64*1024
outputFile = filename+".hacklab"
size = os.path.getsize(filename) #+ 16
filesize = str(size).zfill(16)
IV = Random.new().read(16)
secret = "0000hack1lab0000"
encryptor = AES.new(key, AES.MODE_CBC, IV)
with open(filename, 'rb') as infile:
with open(outputFile, 'wb') as outfile:
outfile.write(filesize.encode('utf-8'))
outfile.write(IV)
if ig != 'True':
outfile.write(encryptor.encrypt(secret))
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' ' * (16 - (len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))
def decrypt(key, filename):
chunksize = 64 * 1024
outputFile = filename.split('.hacklab')[0]
with open(filename, 'rb') as infile:
filesize = int(infile.read(16))
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
with open(outputFile, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
chunk = str(decryptor.decrypt(chunk))
chunk = chunk.replace("0000hack1lab0000", "")
outfile.write(chunk)
outfile.truncate(filesize)
def check(key, filename):
chunksize = 64# * 1024
secret = "0000hack1lab0000"
with open(filename, 'rb') as infile:
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
chunk = infile.read(chunksize)
test = decryptor.decrypt(chunk)
if secret not in test:
exit(red+bold+"[!] Wrong Password!"+end)
def getkey(password):
hasher = SHA256.new(password.encode('utf-8'))
return hasher.digest()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--crypt", help="File Encryption", type=str)
parser.add_argument("-d", "--dcrypt", help="File Decryption", type=str)
parser.add_argument("-p", "--password", help="Password To Encrypt/Decrypt a File", type=str)
parser.add_argument("-x", "--delete", help="Delete The Original File", action='store_true')
parser.add_argument("-i", "--ignore", help="Ignore The Check For The Password.", action='store_true')
args = parser.parse_args()
enc = str(args.crypt)
dec = str(args.dcrypt)
password = str(args.password)
dd = str(args.delete)
ig = str(args.ignore)
if enc == "None" and dec == "None":
parser.print_help()
exit(1)
if password == "None":
password = getpass()
if enc != "None":
print(blue+"[+] Encrypt: "+end+"[ "+enc+" ]")
encrypt(getkey(password), enc, ig)
print(blue+"[+] Output: "+end+"[ "+enc+".hacklab"+" ]")
if dd == "True":
print(red+"[!] Remove: "+end+"[ "+enc+" ]")
os.remove(enc)
print(green+bold+"[*] Done!"+end)
elif dec != "None":
print(blue+"[+] Decrypt: "+end+"[ "+dec+" ]")
if ig != 'True':
check(getkey(password), dec)
decrypt(getkey(password), dec)
name = dec.split(".hacklab")[0]
print(blue+"[+] Output: "+end+"[ "+name+" ]")
if dd == "True":
print(red+"[!] Remove: "+end+"[ "+dec+" ]")
os.remove(dec)
print(green+bold+"[*] Done!"+end)
if __name__ == '__main__':
main()