Skip to content

Commit ea2c79f

Browse files
Merge pull request #528 from shraddha761/crypto
Crypto script
2 parents 5471f33 + 25dbafe commit ea2c79f

3 files changed

Lines changed: 237 additions & 0 deletions

File tree

Crypto/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Crypto
2+
3+
Encrypt/Decrypt your files with password to save your privacy

Crypto/crypto.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
import os, argparse
3+
from getpass import getpass
4+
# sudo pip install pycryptodome==3.6.1
5+
from Crypto.Cipher import AES
6+
from Crypto.Hash import SHA256
7+
from Crypto import Random
8+
9+
10+
green = "\033[32m"
11+
blue = "\033[34m"
12+
red = "\033[31m"
13+
bold = "\033[1m"
14+
end = "\033[0m"
15+
16+
17+
def encrypt(key, filename, ig):
18+
chunksize = 64*1024
19+
outputFile = filename+".hacklab"
20+
size = os.path.getsize(filename) #+ 16
21+
filesize = str(size).zfill(16)
22+
IV = Random.new().read(16)
23+
secret = "0000hack1lab0000"
24+
25+
encryptor = AES.new(key, AES.MODE_CBC, IV)
26+
27+
with open(filename, 'rb') as infile:
28+
with open(outputFile, 'wb') as outfile:
29+
outfile.write(filesize.encode('utf-8'))
30+
outfile.write(IV)
31+
if ig != 'True':
32+
outfile.write(encryptor.encrypt(secret))
33+
34+
while True:
35+
chunk = infile.read(chunksize)
36+
37+
if len(chunk) == 0:
38+
break
39+
elif len(chunk) % 16 != 0:
40+
chunk += b' ' * (16 - (len(chunk) % 16))
41+
42+
outfile.write(encryptor.encrypt(chunk))
43+
44+
45+
def decrypt(key, filename):
46+
chunksize = 64 * 1024
47+
outputFile = filename.split('.hacklab')[0]
48+
49+
50+
with open(filename, 'rb') as infile:
51+
filesize = int(infile.read(16))
52+
IV = infile.read(16)
53+
decryptor = AES.new(key, AES.MODE_CBC, IV)
54+
55+
with open(outputFile, 'wb') as outfile:
56+
57+
while True:
58+
chunk = infile.read(chunksize)
59+
60+
if len(chunk) == 0:
61+
break
62+
63+
chunk = str(decryptor.decrypt(chunk))
64+
chunk = chunk.replace("0000hack1lab0000", "")
65+
outfile.write(chunk)
66+
outfile.truncate(filesize)
67+
68+
69+
def check(key, filename):
70+
chunksize = 64# * 1024
71+
secret = "0000hack1lab0000"
72+
73+
with open(filename, 'rb') as infile:
74+
IV = infile.read(16)
75+
decryptor = AES.new(key, AES.MODE_CBC, IV)
76+
77+
chunk = infile.read(chunksize)
78+
test = decryptor.decrypt(chunk)
79+
if secret not in test:
80+
exit(red+bold+"[!] Wrong Password!"+end)
81+
82+
83+
84+
85+
def getkey(password):
86+
hasher = SHA256.new(password.encode('utf-8'))
87+
return hasher.digest()
88+
89+
def main():
90+
parser = argparse.ArgumentParser()
91+
parser.add_argument("-e", "--crypt", help="File Encryption", type=str)
92+
parser.add_argument("-d", "--dcrypt", help="File Decryption", type=str)
93+
parser.add_argument("-p", "--password", help="Password To Encrypt/Decrypt a File", type=str)
94+
parser.add_argument("-x", "--delete", help="Delete The Original File", action='store_true')
95+
parser.add_argument("-i", "--ignore", help="Ignore The Check For The Password.", action='store_true')
96+
args = parser.parse_args()
97+
98+
enc = str(args.crypt)
99+
dec = str(args.dcrypt)
100+
password = str(args.password)
101+
dd = str(args.delete)
102+
ig = str(args.ignore)
103+
104+
if enc == "None" and dec == "None":
105+
parser.print_help()
106+
exit(1)
107+
108+
if password == "None":
109+
password = getpass()
110+
111+
if enc != "None":
112+
print(blue+"[+] Encrypt: "+end+"[ "+enc+" ]")
113+
encrypt(getkey(password), enc, ig)
114+
print(blue+"[+] Output: "+end+"[ "+enc+".hacklab"+" ]")
115+
if dd == "True":
116+
print(red+"[!] Remove: "+end+"[ "+enc+" ]")
117+
os.remove(enc)
118+
print(green+bold+"[*] Done!"+end)
119+
120+
elif dec != "None":
121+
print(blue+"[+] Decrypt: "+end+"[ "+dec+" ]")
122+
if ig != 'True':
123+
check(getkey(password), dec)
124+
decrypt(getkey(password), dec)
125+
name = dec.split(".hacklab")[0]
126+
print(blue+"[+] Output: "+end+"[ "+name+" ]")
127+
if dd == "True":
128+
print(red+"[!] Remove: "+end+"[ "+dec+" ]")
129+
os.remove(dec)
130+
print(green+bold+"[*] Done!"+end)
131+
132+
133+
134+
if __name__ == '__main__':
135+
main()

0 commit comments

Comments
 (0)