Skip to content

Commit

Permalink
Encryption for auto generated names
Browse files Browse the repository at this point in the history
  • Loading branch information
MPCodeWriter21 committed Feb 28, 2022
1 parent 047e4f6 commit 2096a68
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

### 2.4.2

Encryption for auto generated names

### 2.4.1

Added auto name generation for output files
Expand Down
2 changes: 1 addition & 1 deletion InvisibleCharm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from log21 import get_colors as _gc
from InvisibleCharm.lib.Console import exit as _exit

__version__ = "2.4.1"
__version__ = "2.4.2"
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
__github__ = "Https://GitHub.com/MPCodeWriter21/InvisibleCharm"

Expand Down
53 changes: 38 additions & 15 deletions InvisibleCharm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# CodeWriter21

import os

from getpass import getpass

import PIL
Expand All @@ -11,23 +12,42 @@
from log21 import get_colors as gc, ColorizingArgumentParser

from InvisibleCharm.Settings import banner, is_windows, operating_system
from InvisibleCharm.lib.data.Encryption import encrypt_aes, decrypt_aes
from InvisibleCharm.lib.Console import logger, input, verbose, quiet, exit
from InvisibleCharm.lib.operations import ntfs_embed, win_extract, win_attrib_hide, win_attrib_reveal, to_image_file, \
from InvisibleCharm.lib.operations import ntfs_embed, ntfs_extract, win_attrib_hide, win_attrib_reveal, to_image_file, \
from_image_file, embed_file, extract_file
from InvisibleCharm.lib.Exceptions import CoverDataTypeNotFoundError, InvalidCoverDataTypeError, \
NoEmbeddedFileFoundError
from InvisibleCharm.lib.File import get_names


def generate_destination_path(source_path: str, extension: str = None):
def generate_destination_path(source_path: str, extension: str = None, aes_pass: str = None, encrypt: bool = True):
"""
Generates a destination path based on the source path.
:param source_path: The source path.
:param extension: The extension to use.
:param aes_pass: The AES password.
:param encrypt: Whether to encrypt or decrypt the file name.
:return: The destination path.
"""
directory = os.path.dirname(source_path)
name, ext = os.path.splitext(os.path.basename(source_path))
if aes_pass:
try:
if encrypt:
name_ = bytes.hex(encrypt_aes(name, aes_pass))
else:
name_ = decrypt_aes(bytes.fromhex(name), aes_pass).decode()
with open(os.path.join(directory, name_ + 'tmp' + ext), 'x'):
pass
if os.path.exists(os.path.join(directory, name_ + 'tmp' + ext)):
os.remove(os.path.join(directory, name_ + 'tmp' + ext))
name = name_
except ValueError or OSError or FileNotFoundError:
logger.error(gc("red") + f" ! Error: Failed to {'encrypt' if encrypt else 'decrypt'} the file name." +
gc("reset"))
except FileExistsError:
pass
dest_path = os.path.join(directory, name + '{}' + (extension if extension is not None else ext))
number = ''
if os.path.exists(dest_path.format(number)):
Expand Down Expand Up @@ -112,17 +132,6 @@ def main():
exit(gc("lr") + f' ! Error: Image Mode: `{gc("lw")}{args.image_mode}{gc("lr")}` not found!\n' +
f' + Valid values:{gc("lb")} 3{gc("lr")},{gc("lb")} 4')

if (args.ntfs_embed or args.to_image or args.embed) and not args.destination:
logger.warning(gc("lr") + " ! Warning: You should set destination path for this operation. use: --dest-file/-d")
logger.info(gc('ly') + " = Generating a destination path to save the output...")
if args.mode.lower() in ['hide', 'h']:
if args.to_image:
args.destination = generate_destination_path(args.source, '.png')
else:
args.destination = generate_destination_path(args.source)
elif args.mode.lower() in ['reveal', 'r']:
args.destination = generate_destination_path(args.source, '')

if args.destination:
# Makes sure that destination directory exists
if os.path.split(args.destination)[0]:
Expand All @@ -132,6 +141,20 @@ def main():
if args.aes_encryption and not args.aes_encryption_pass:
args.aes_encryption_pass = getpass(" * Enter a password for AES encryption: ")

if (args.ntfs_embed or args.to_image or args.embed) and not args.destination:
logger.warning(gc("lr") + " ! Warning: You should set destination path for this operation. use: --dest-file/-d")
logger.info(gc('ly') + " = Generating a destination path to save the output...")
if args.mode.lower() in ['hide', 'h']:
if args.to_image:
args.destination = generate_destination_path(args.source, '.png',
aes_pass=args.aes_encryption_pass or 'P@$$w0rd')
else:
args.destination = generate_destination_path(args.source,
aes_pass=args.aes_encryption_pass or 'P@$$w0rd')
elif args.mode.lower() in ['reveal', 'r']:
args.destination = generate_destination_path(args.source, '',
aes_pass=args.aes_encryption_pass or 'P@$$w0rd', encrypt=False)

# Reads the RSA key file if given
rsa_key = None
if args.rsa_encryption_key:
Expand Down Expand Up @@ -237,8 +260,8 @@ def main():
logger.print(gc("lb") + 'Available names' + gc("lr") + ': ' + gc("lg") +
f'{gc("lr")}, {gc("lg")}'.join(possible_names))
name = input(gc("ly") + f'Enter the name of embedded file' + gc("lr") + ': ' + gc("lg"))
win_extract(args.source, args.destination, args.delete, args.compress, name, args.aes_encryption_pass,
rsa_key)
ntfs_extract(args.source, args.destination, args.delete, args.compress, name, args.aes_encryption_pass,
rsa_key)
elif args.win_attrib:
if args.destination:
logger.warn(gc("lr") + ' ! Warning: ' + gc("blm", "gr") +
Expand Down
6 changes: 3 additions & 3 deletions InvisibleCharm/lib/operations/Windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from InvisibleCharm.lib.Exceptions import WinEmbeddedFileNotFoundError as _WinEmbeddedFileNotFoundError, \
NoWinEmbeddedFileFoundError as _NoWinEmbeddedFileFoundError

__all__ = ['ntfs_embed', 'win_extract', 'win_attrib_hide', 'win_attrib_reveal']
__all__ = ['ntfs_embed', 'ntfs_extract', 'win_attrib_hide', 'win_attrib_reveal']


# Hides a file in another Windows file
Expand Down Expand Up @@ -99,8 +99,8 @@ def ntfs_embed(source: str, dest: str, delete_source: bool, compress: bool, cove


# Extracts a hidden file from a file in windows
def win_extract(source: str, dest: str, delete_source: bool, compress: bool, name: str = '',
encrypt_pass: _Union[str, bytes] = '', rsa_encrypt_key: _RSA.RsaKey = None) -> None:
def ntfs_extract(source: str, dest: str, delete_source: bool, compress: bool, name: str = '',
encrypt_pass: _Union[str, bytes] = '', rsa_encrypt_key: _RSA.RsaKey = None) -> None:
"""
Extracts a hidden file from a file in Windows.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ options:
Changes
-------

### 2.4.1
### 2.4.2

Added auto name generation for output files
Encryption for auto generated names

[Full Changelog](CHANGELOG.md)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
LICENSE = file.read()

DESCRIPTION = 'InvisibleCharm is a python script that allows you to hide your files.'
VERSION = '2.4.1'
VERSION = '2.4.2'
REQUIREMENTS = ['log21>=1.5.1', 'Pillow>=8.3.2', 'pycryptodome>=3.12.0', 'importlib_resources>=5.2.2']
if os.name == 'nt':
REQUIREMENTS.append('python-magic-bin>=0.4.14')
Expand Down

0 comments on commit 2096a68

Please sign in to comment.