1
0
Fork 0
mirror of https://github.com/HACKERALERT/Picocrypt.git synced 2024-09-20 09:46:43 +00:00

Update Picocrypt.py

This commit is contained in:
Evan Su 2021-03-22 15:03:19 -04:00 committed by GitHub
parent 31f9613591
commit 2f940d3dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,12 +22,13 @@ from Crypto.Hash import SHA3_512 as sha3_512
from secrets import compare_digest from secrets import compare_digest
from os import urandom,fsync,remove,system from os import urandom,fsync,remove,system
from os.path import getsize,expanduser,isdir from os.path import getsize,expanduser,isdir
from os.path import dirname,abspath from os.path import dirname,abspath,realpath
from os.path import join as pathJoin from os.path import join as pathJoin
from os.path import split as pathSplit from os.path import split as pathSplit
from tkinterdnd2 import TkinterDnD,DND_FILES from tkinterdnd2 import TkinterDnD,DND_FILES
from zipfile import ZipFile from zipfile import ZipFile
from pathlib import Path from pathlib import Path
from shutil import rmtree
import sys import sys
import tkinter import tkinter
import tkinter.ttk import tkinter.ttk
@ -45,6 +46,7 @@ except:
pass pass
# Global variables and notices # Global variables and notices
rootDir = dirname(realpath(__file__))
inputFile = "" inputFile = ""
outputFile = "" outputFile = ""
outputPath = "" outputPath = ""
@ -55,7 +57,8 @@ working = False
gMode = None gMode = None
headerRsc = None headerRsc = None
draggedFiles = False draggedFiles = False
dragFolderPath = False draggedFolderPaths = False
filesToErase = False
adString = "File metadata (used to store some text along with the file):" adString = "File metadata (used to store some text along with the file):"
compressingNotice = "Compressing files together..." compressingNotice = "Compressing files together..."
passwordNotice = "Error. The provided password is incorrect." passwordNotice = "Error. The provided password is incorrect."
@ -68,6 +71,7 @@ kVeryCorruptedNotice = "The input file is badly corrupted, but the output has be
derivingNotice = "Deriving key (takes a few seconds)..." derivingNotice = "Deriving key (takes a few seconds)..."
keepNotice = "Keep decrypted output even if it's corrupted or modified" keepNotice = "Keep decrypted output even if it's corrupted or modified"
eraseNotice = "Securely erase and delete original file" eraseNotice = "Securely erase and delete original file"
erasingNotice = "Securely erasing original file(s)..."
overwriteNotice = "Output file already exists. Would you like to overwrite it?" overwriteNotice = "Output file already exists. Would you like to overwrite it?"
rsNotice = "Prevent corruption using Reed-Solomon" rsNotice = "Prevent corruption using Reed-Solomon"
rscNotice = "Creating Reed-Solomon tables..." rscNotice = "Creating Reed-Solomon tables..."
@ -94,7 +98,7 @@ s.configure("TCheckbutton",background="#f5f6f7")
# Event when user selects an input file # Event when user selects an input file
def inputSelected(draggedFile=None): def inputSelected(draggedFile=None):
global inputFile,working,headerRsc,draggedFiles global inputFile,working,headerRsc,draggedFiles
global dragFolderPath global draggedFolderPaths,filesToErase
dummy.focus() dummy.focus()
status.config(cursor="") status.config(cursor="")
status.bind("<Button-1>",lambda e:None) status.bind("<Button-1>",lambda e:None)
@ -102,7 +106,8 @@ def inputSelected(draggedFile=None):
# Try to handle when select file is cancelled # Try to handle when select file is cancelled
try: try:
draggedFiles = None draggedFiles = None
dragFolderPath = None filesToErase = None
draggedFolderPaths = []
# Ask for input file # Ask for input file
suffix = "" suffix = ""
if not draggedFile: if not draggedFile:
@ -114,20 +119,47 @@ def inputSelected(draggedFile=None):
raise Exception("No file selected.") raise Exception("No file selected.")
inputFile = tmp inputFile = tmp
else: else:
# Check if multiple files tmp = [i for i in draggedFile]
if "} {" in draggedFile: res = []
draggedFiles = draggedFile[1:-1] within = False
draggedFiles = draggedFiles.split("} {") tmpName = ""
elif " " in draggedFile and "{" not in draggedFile: for i in tmp:
draggedFiles = draggedFile.split() if i=="{":
else: within = True
if isdir(draggedFile): elif i=="}":
draggedFiles = Path(draggedFile).rglob("*") within = False
draggedFiles = [abspath(i) for i in draggedFiles] res.append(tmpName)
dragFolderPath = draggedFile tmpName = ""
else: else:
draggedFile = draggedFile.replace("{","") print(tmpName)
inputFile = draggedFile.replace("}","") if i==" " and not within:
if tmpName!="":
res.append(tmpName)
tmpName = ""
else:
tmpName += i
if tmpName:
res.append(tmpName)
draggedFiles = []
filesToErase = []
for i in res:
if isdir(i):
draggedFolderPaths.append(i)
tmp = Path(i).rglob("*")
for p in tmp:
draggedFiles.append(abspath(p))
else:
filesToErase.append(i)
draggedFiles = list(filter(lambda i:not isdir(i),draggedFiles))
if len(draggedFiles)==1:
draggedFiles = []
inputFile = draggedFiles[0]
else:
inputFile = ""
# Decide if encrypting or decrypting # Decide if encrypting or decrypting
if ".pcv" in inputFile.split("/")[-1]: if ".pcv" in inputFile.split("/")[-1]:
@ -183,7 +215,7 @@ def inputSelected(draggedFile=None):
# Show selected file(s) # Show selected file(s)
if draggedFiles: if draggedFiles:
inputString.set(f"{len(draggedFiles)} files selected"+ inputString.set(f"{len(draggedFiles)} file(s) selected"+
" (will encrypt).") " (will encrypt).")
else: else:
inputString.set(inputFile.split("/")[-1]+suffix) inputString.set(inputFile.split("/")[-1]+suffix)
@ -212,6 +244,7 @@ def inputSelected(draggedFile=None):
# Allow drag and drop # Allow drag and drop
def onDrop(e): def onDrop(e):
print(e.data)
inputSelected(e.data) inputSelected(e.data)
tk.drop_target_register(DND_FILES) tk.drop_target_register(DND_FILES)
tk.dnd_bind("<<Drop>>",onDrop) tk.dnd_bind("<<Drop>>",onDrop)
@ -290,7 +323,7 @@ cpasswordInput["state"] = "disabled"
# Start the encryption/decryption process # Start the encryption/decryption process
def start(): def start():
global inputFile,outputFile,password,ad,kept global inputFile,outputFile,password,ad,kept
global working,gMode,headerRsc,draggedFiles global working,gMode,headerRsc,draggedFiles,filesToErase
global dragFolderPath global dragFolderPath
dummy.focus() dummy.focus()
reedsolo = False reedsolo = False
@ -347,19 +380,13 @@ def start():
if draggedFiles: if draggedFiles:
statusString.set(compressingNotice) statusString.set(compressingNotice)
tmp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") tmp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
if dragFolderPath: zfPath = Path(draggedFiles[0]).parent.absolute()
zfName = Path(dragFolderPath).parent.absolute() zfOffset = len(str(zfPath))
zfName = pathJoin(zfName,tmp+".zip") zfName = pathJoin(zfPath,tmp+".zip")
else:
zfName = Path(draggedFiles[0]).parent.absolute()
zfName = pathJoin(zfName,tmp+".zip")
zf = ZipFile(zfName,"w") zf = ZipFile(zfName,"w")
for i in draggedFiles: for i in draggedFiles:
if dragFolderPath: zf.write(i,i[zfOffset:])
nameOffset = len(dragFolderPath)
zf.write(i,i[nameOffset:])
else:
zf.write(i,pathSplit(i)[1])
zf.close() zf.close()
inputFile = zfName inputFile = zfName
outputFile = zfName+".pcv" outputFile = zfName+".pcv"
@ -474,7 +501,7 @@ def start():
password, password,
salt, salt,
time_cost=8, # 8 iterations time_cost=8, # 8 iterations
memory_cost=2**20, # 2^20 Kibibytes (1GiB) memory_cost=2**10, # 2^20 Kibibytes (1GiB)
parallelism=8, # 8 parallel threads parallelism=8, # 8 parallel threads
hash_len=32, hash_len=32,
type=Type.ID type=Type.ID
@ -687,17 +714,26 @@ def start():
fout.close() fout.close()
fin.close() fin.close()
print(draggedFolderPaths)
print(draggedFiles)
print(filesToErase)
input()
# Securely wipe files as necessary # Securely wipe files as necessary
if wipe: if wipe:
progress.config(mode="indeterminate") if draggedFolderPaths:
progress.start(15) for i in draggedFolderPaths:
if draggedFiles:
for i in draggedFiles:
print("==============="+i)
secureWipe(i) secureWipe(i)
if filesToErase:
for i in range(len(filesToErase)):
statusString.set(erasingNotice+f" ({i}/{len(filesToErase)}")
progress["value"] = i/len(filesToErase)
print("Erasing file "+filesToErase[i])
secureWipe(filesToErase[i])
secureWipe(inputFile) secureWipe(inputFile)
# Secure wipe not enabled
else: else:
if draggedFiles: if draggedFiles:
# Remove temporary zip file if created
remove(inputFile) remove(inputFile)
# Show appropriate notice if file corrupted or modified # Show appropriate notice if file corrupted or modified
@ -763,10 +799,25 @@ def startWorker():
# Securely wipe file # Securely wipe file
def secureWipe(fin): def secureWipe(fin):
statusString.set("Securely erasing original file...") global draggedFiles
statusString.set(erasingNotice)
# Check platform, erase accordingly # Check platform, erase accordingly
if platform.system()=="Windows": if platform.system()=="Windows":
system(f'sdelete64.exe "{fin}" -p 4') if isdir(fin):
paths = []
for i in Path(fin).rglob("*"):
if dirname(i) not in paths:
paths.append(dirname(i))
for i in range(len(paths)):
statusString.set(erasingNotice+f" ({i}/{len(paths)})")
progress["value"] = 100*i/len(paths)
system(f'cd "{paths[i]}" && "{rootDir}/sdelete64.exe" * -p 4 -s -nobanner')
system(f'cd "{i}"')
rmtree(fin)
else:
statusString.set(erasingNotice)
progress["value"] = 100
system(f'sdelete64.exe "{fin}" -p 4 -nobanner')
elif platform.system()=="Darwin": elif platform.system()=="Darwin":
pass pass
else: else:
@ -982,7 +1033,7 @@ def createRsc():
sys.exit(0) sys.exit(0)
def prepare(): def prepare():
system("sdelete64.exe /accepteula") system("sdelete64.exe /accepteula -nobanner")
# Close window only if not encrypting or decrypting # Close window only if not encrypting or decrypting
def onClose(): def onClose():