#!
/usr/bin/env python
# -*- coding: utf-8 -*-
PATH = "default"
OUTPUT = "default"
MODE = "w"
# This is the main script of the steal-all-files github repository.
#
# This script has been created by Pablo Corbalán De Concepión | tw: @pablocorbcon
#
# You can read the License for this script in the "LICENSE" file of this folder.
#
# For more information, visit: github.com/pblcc/steal-all-files (if you have access
to it)
from rich.markdown import Markdown
from rich.console import Console
import sys
import os
import zipfile
import platform
console = Console()
def exitapp(**kwargs):
"""
This function is used to exit the app using the sys.exit function
Parameters
----------
kwargs: **
Some keywords for closing the app
{
code: int
The exit code
msg: str
The message if there is a message
}
"""
# check if the user has actually used keywords
keys = kwargs.keys()
if "msg" in keys:
k = kwargs["msg"]
print(k)
code = 0 if "code" not in keys else kwargs["code"]
sys.exit(0)
def readfile(file_route):
"""
Reads a file, handles it's exceptions and then returns the content of
that file.
Parameters
----------
file_route: str
The file route for reading all
Returns
-------
The content of file_route
"""
try:
with open(file_route) as f:
return f.read()
except FileNotFoundError as e:
print("We can't find a file! :500:")
exitapp(-1, e)
except Exception as e:
exitapp(-1, e)
def displayhelp():
"""
This function is used for displaying help and the commands about the
application
For running this function use: -h
"""
f = readfile("help.txt")
print(f)
def displaylicense():
"""
This function is used for displaying the License of this app.
For running this function use: -l
"""
f = readfile("LICENSE.md")
console.print(Markdown(f))
def systemis(system_to_check):
"""
Get's the system (os) and returns True if the system is
:param system_to_check:
Parameters
----------
system_to_check: str
The system that has to be checked
Returns true if the system to be checked is the actual os
"""
system = platform.system().lower()
return True if system == system_to_check else False
def setuppath():
"""
Set's up the path depending on the operative system the user is using.
"""
global PATH
if PATH == "default":
PATH = "C://" if systemis("windows") else "/home"
def setupoutput():
"""
Set's up the output depending on the path
"""
global OUTPUT, PATH
if OUTPUT == "default":
npath = PATH.replace("/", "_")[1:]
OUTPUT = f"{npath}.zip"
def zipdir(path, ziph):
"""
This is the function that actually walks the directory and extracts the files
inside a ZipFile using the zipfile library.
Parameters
----------
path: str
The relative or absolute path for the directory
ziph: zipfile.ZipFile
The zipfile handler
"""
for root, dirs, files in os.walk(path):
for file in files:
f = os.path.join(root, file)
ziph.write(f, os.path.relpath(f, os.path.join(path, "..")))
def isarg(arg, value=False):
"""
Returns True if the argument :arg: is an actual argument
Parameters
----------
arg: str
The argument to be checked
value: bool
True if :arg: should have a value
"""
is_arg = False
arg_value = None
args = sys.argv
if not value:
if arg in args:
is_arg = True
else:
# We also have to check if the argument has a value to then return it
for i, argument in enumerate(args):
if argument == arg:
if i + 1 < len(args):
arg_value = args[i + 1]
is_arg = True
# Return a tupple with all this information
return (is_arg, arg_value)
def readargs():
"""
This function reads the arguments of the application using the system library
to
display them and set up the global configuration variables using :func isarg:
This function does'nt take any parameters nor returns any value
"""
global PATH, OUTPUT
# Check out the flags
if isarg("-h")[0]:
# The user simply wants to display some help and information about the
program
displayhelp()
exitapp()
if isarg("-l")[0]:
displaylicense()
exitapp()
if isarg("-p", True)[0]:
PATH = isarg("-p", True)[1]
if isarg("-o", True)[0]:
o = isarg("-o", True)[1]
OUTPUT = f"{o}.zip"
if isarg("-rw")[0]:
MODE = ""
if __name__ == "__main__":
setuppath()
setupoutput()
readargs()
console.print(
f"Extracting {PATH} at {OUTPUT} - Press CTRL + C to stop the process."
)
z = zipfile.ZipFile(OUTPUT, MODE, zipfile.ZIP_DEFLATED)
zipdir(PATH, z)
z.close()
def transposition_cipher_encrypt(text, key):
ciphertext = [""] * int(key)
for column in range(int(key)):
currentIndex = column
while currentIndex < len(text):
ciphertext[column] += text[currentIndex]
currentIndex += int(key)
return "".join(ciphertext)
def transposition_cipher_decrypt(ciphertext, key):
# Determine the number of columns based on the key
num_columns = int(key)
# Determine the number of rows based on the ciphertext length and key size
num_rows = -(-len(ciphertext) // num_columns)
# Create an empty grid (list of lists) for the transposition
grid = [[''] * num_columns for _ in range(num_rows)]
# Fill in the grid with the characters from the ciphertext
index = 0
for col in range(num_columns):
for row in range(num_rows):
if index < len(ciphertext):
grid[row][col] = ciphertext[index]
index += 1
# Extract the decrypted text from the grid
decrypted_text = ''
for row in range(num_rows):
for col in range(num_columns):
decrypted_text += grid[row][col]
return decrypted_text
def main():
print("Choose an option:")
print("1. Encrypt using Caesar cipher")
print("2. Decrypt using Caesar cipher")
print("3. Encrypt using Affine cipher")
print("4. Decrypt using Affine cipher")
print("5. Encrypt using Substitution cipher")
print("6. Decrypt using Substitution cipher")
print("7. Encrypt using Transposition cipher")
print("8. Decrypt using Transposition cipher")
choice = input("Enter your choice (1-8): ")
text = input("Enter the text: ")
if choice == '1':
try:
shift = int(input("Enter the shift value for Caesar cipher: "))
encrypted_text = caesar_cipher_encrypt(text, shift)
print("Caesar cipher (encryption):", encrypted_text)
except ValueError:
print("Invalid Input")
elif choice == '2':
try:
shift = int(input("Enter the shift value for Caesar cipher: "))
decrypted_text = caesar_cipher_decrypt(text, shift)
print("Caesar cipher (decryption):", decrypted_text)
except ValueError:
print("Invalid Input")
elif choice == '3':
try:
a = int(input("Enter the value for 'a' in Affine cipher: "))
b = int(input("Enter the value for 'b' in Affine cipher: "))
encrypted_text = affine_cipher_encrypt(text, a, b)
print("Affine cipher (encryption):", encrypted_text)
except ValueError:
print("Invalid Input")
elif choice == '4':
try:
a = int(input("Enter the value for 'a' in Affine cipher: "))
b = int(input("Enter the value for 'b' in Affine cipher: "))
decrypted_text = affine_cipher_decrypt(text, a, b)
print("Affine cipher (decryption):", decrypted_text)
except ValueError:
print("Invalid Input")
elif choice == '5':
key = input("Enter the substitution key: ")
if len(key) == 26:
encrypted_text = substitution_cipher_encrypt(text, key)
print("Substitution cipher (encryption):", encrypted_text)
else:
print("Key must have the same length as the number of characters in the
alphabet (26).")
elif choice == '6':
key = input("Enter the substitution key: ")
if len(key) == 26:
decrypted_text = substitution_cipher_decrypt(text, key)
print("Substitution cipher (decryption):", decrypted_text)
else:
print("Key must have the same length as the number of characters in the
alphabet (26).")
elif choice == '7':
try:
transpose_key = input(
"Enter the transposition key (make sure its less than length of
stirng): "
)
if int(transpose_key) > len(text):
print("Key must be less than length of string")
else:
encrypted_text = transposition_cipher_encrypt(text, transpose_key)
print("Transposition cipher (encryption):", encrypted_text)
except ValueError:
print("Invalid Input")
elif choice == '8':
try:
transpose_key = input(
"Enter the transposition key (make sure its less than length of
stirng): "
)
if int(transpose_key) > len(text):
print("Key must be less than length of string")
else:
decrypted_text = transposition_cipher_decrypt(text, transpose_key)
print("Transposition cipher (decryption):", decrypted_text)
except ValueError:
print("Invalid Input")
else:
print("Invalid choice. Please choose a number between 1 and 8.")
if __name__ == "__main__":
main()