Created
December 3, 2019 02:55
-
-
Save rhovelz/3f6d4872e3da291d2ff0601537990a1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from Crypto.Cipher import AES | |
from Crypto.Hash import SHA256 | |
from Crypto import Random | |
#author @corshine | |
print("[+] CORSHINE ENCRYPTION AND DECRYPTION TOOL.\n[+] Choose the options below.") | |
def encrypt(key, filename): | |
chunksize = 64 * 1024 | |
outputFile = 'encrypted' + filename | |
filesize = str(os.path.getsize(filename)).zfill(16) | |
IV = Random.new().read(16) | |
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) | |
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[11:] | |
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 | |
outfile.write(decryptor.decrypt(chunk)) | |
outfile.truncate(filesize) | |
def getKey(password): | |
hasher = SHA256.new(password.encode('utf-8')) | |
return hasher.digest() | |
def Main(): | |
choice = input("(E)ncrypt or (D)ecrypt: ") | |
if choice == 'E' or choice == 'e': | |
filename = input("File to encrypt: ") | |
password = input("Password: ") | |
encrypt(getKey(password), filename) | |
print("Done.") | |
elif choice == 'D' or choice == 'd': | |
filename = input("File to decrypt: ") | |
password = input("Password: ") | |
decrypt(getKey(password), filename) | |
print("Done.") | |
else: | |
print("Please select the right option") | |
if __name__ == '__main__': | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment