Datei in eine andere Datei 'verstecken'
Verfasst: Samstag 11. August 2018, 12:55
Hey Leute, ich habe mal ein kleines Programm erstellt, mit dem man eine Datei in einer anderen 'verstecken' kann. Naja da ich gerne Feedback zum Code hätte (Anti pattern usw.) stelle ich das einfach mal vor:
Code: Alles auswählen
import argparse
import os
PYGANOS = '''
/$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$$
/$$__ $$| $$ | $$ /$$__ $$ |____ $$| $$__ $$ /$$__ $$ /$$_____/
| $$ \ $$| $$ | $$| $$ \ $$ /$$$$$$$| $$ \ $$| $$ \ $$| $$$$$$
| $$ | $$| $$ | $$| $$ | $$ /$$__ $$| $$ | $$| $$ | $$ \____ $$
| $$$$$$$/| $$$$$$$| $$$$$$$| $$$$$$$| $$ | $$| $$$$$$/ /$$$$$$$/
| $$____/ \____ $$ \____ $$ \_______/|__/ |__/ \______/ |_______/
| $$ /$$ | $$ /$$ \ $$
| $$ | $$$$$$/| $$$$$$/ A simple hiding program.
|__/ \______/ \______/
'''
def pack(file1, file2):
#to save the file names which are important for the output
output = file1
hide_name = file2
print(f"\n\nHiding {file2} in {file1}.")
file1 = open(file1, 'rb').read()
file2 = open(file2, 'rb').read()
with open(f'output/{output}', 'wb') as data:
data.write(file1)
#when unpack then it is the starting poin and here also the name of the file is saved
data.write(f'\npyganos-start\n{hide_name}\n'.encode())
data.write(file2)
print("\nDone!")
def unpack(output):
is_pyganos = False
with open(output, 'rb') as file:
searchlines = file.readlines()
for i, line in enumerate(searchlines):
if 'pyganos-start'.encode() in line:
filename = searchlines[i+1].decode().strip()
unpacked = searchlines[i+2:]
is_pyganos = True
print(f"\n\nSuccesfully unhide {filename} - Look into the output folder, there will be {filename}!")
if is_pyganos == True:
with open(f'output/{filename}', 'wb') as output:
for line in unpacked:
output.write(line)
else:
print('\n\nIt isn\'t a pyganos File! Please try an other one.')
def checkdir():
if not os.path.isdir('output'):
os.mkdir('output')
cmdinput = argparse.ArgumentParser(description='A program which can hide one file in an other.')
cmdinput.add_argument('--h', '--hide', nargs = 2, help='This command is for packing /hiding the data. You have to insert two files. Eg. python pyganos.py -h file.png file2.txt')
cmdinput.add_argument('--u', '--unhide', nargs = 1, help='This command is for unpacking you hiding data! Usage python pyganos -u file1.png - The output will be saved in the folder output.')
args = vars(cmdinput.parse_args())
checkdir()
if args['h']:
print(PYGANOS)
filenames = args['h']
file1 = filenames[0]
file2 = filenames[1]
pack(file1, file2)
elif args['u']:
print(PYGANOS)
filename = args['u']
unpack(filename[0])
else:
print(PYGANOS)
while True:
user = input('Do you like to hide or unhide a file? [H|U]> ')
if user.upper() == 'H':
while True:
file1_exists = False
file2_exists = False
file1 = input('Your first file in which the second will be hided (If not in same dir an pyganos, please give in also the dir!)> ')
if os.path.exists(file1):
file1_exists = True
file2 = input('You file which you would like to hide (If not in same dir an pyganos, please give in also the dir!)> ')
if os.path.exists(file2):
file2_exists = True
if file1_exists == False and file2_exists == False:
print(f'Can\'t find {file1} and {file2}!\n')
elif file1_exists == False:
print(f'Can\'t find {file1}!\note')
elif file2_exists == False:
print(f'Can\'t find {file2}!\n')
else:
break
pack(file1, file2)
break
elif user.upper() == 'U':
while True:
file_exists = False
file = input('Which file you\'d like to unhide?> ')
if os.path.exists(file):
file_exists = True
if file_exists == False:
print(f'Can\'t find {file}!\n')
else:
break
unpack(file)
break
else:
print('Please insert H or U !')