از شهریور 1400 به توسعه وب علاقه شدیدی پیدا کردم و در حال یادگیری و آموزش توسعه وب هستم.
سورس برنامه رمزنگاری و رمزگشایی فایل به زبان پایتون
امروز سورس برنامه رمزنگاری و رمزگشایی فایل ها رو که به زبان پایتون نوشتم رو در اختیارتون میذارم. امید وارم براتون مفید باشه.
توجه: برای استفاده از سورس باید سه کتابخونه: pyAesCrypt و prettytable و colorama رو نصب کنید.
نحوه نصبشون هم خیلی آسونه فقط کافیه cmd رو بازکنید و با دستور cd به مسیر نصب پایتون برید و با دستور pip instal نصبشون کنید.
مثال:
pip install colorama
سورس
import os
import sys
import pyAesCrypt
from prettytable import PrettyTable
from colorama import init, AnsiToWin32, Fore, Back
#-------------------------------------------------------------------------
init(wrap=False)
stream = AnsiToWin32(sys.stderr).stream
bufferSize = 64 * 1024
password = ''"
#-------------------------------------------------------------------------
def clearScreen():
os.system('cls')
#-------------------------------------------------------------------------
def empty(string)
if not string or string.isspace()
return True
else:
return False
#-------------------------------------------------------------------------
def printEnterAnyKey():
print()
changeForeColor(Fore.WHITE)
res = input("Do you want to continue? (yes/no) ")
if not res == 'yes':
os.system("exit")
#-------------------------------------------------------------------------
def printText(msg):
changeForeColor(Fore.WHITE)
print(msg)
def printError(error):
changeForeColor(Fore.RED)
if not error:
print("An Error Occurred!")
else:
print(error)
def printSucess(msg):
changeForeColor(Fore.GREEN)
print(msg)
#-------------------------------------------------------------------------
def getDesktopPath():
return ("C:\\" + os.path.join(os.environ["HOMEPATH"], "Desktop"))
#-------------------------------------------------------------------------
def changeForeColor(color):
print(color, end='', file=stream)
#-------------------------------------------------------------------------
def saveInDesktop():
changeForeColor(Fore.YELLOW)
res = input('Do you want to save key file in desktop? (yes/no) ')
if res=='yes':
return True
else:
return False
#-------------------------------------------------------------------------
def readTxtFile(path):
return open(path, "rb").read()
#-------------------------------------------------------------------------
def encrypt():
clearScreen()
filePath = ""
source = ""
dest = ""
global password
changeForeColor(Fore.YELLOW)
print("Encrypt Files".center(30, "-"))
select = 0
while select<1 or select>2:
changeForeColor(Fore.LIGHTGREEN_EX)
print()
print("1- Encrypt File")
print("2- Encrypt Folder Content (Files)")
print()
changeForeColor(Fore.LIGHTBLUE_EX)
inp = input("Enter Option Number: ")
print()
if inp.isdigit():
select = int(inp)
if select<0 or select>2:
continue
if select==1:
while not os.path.exists(filePath) or not os.path.isfile(filePath):
changeForeColor(Fore.YELLOW)
print("Enter a valid file path:")
changeForeColor(Fore.WHITE)
filePath = input()
fileDir, fileName = os.path.split(filePath)
source = filePath
if fileDir.endswith("/") or fileDir.endswith("\\"):
dest = fileDir + fileName + ".aes"
else:
dest = fileDir + "/" + fileName + ".aes"
while empty(password):
changeForeColor(Fore.LIGHTBLUE_EX)
print()
password = input("Please Enter Your Password: ")
changeForeColor(Fore.WHITE)
try:
changeForeColor(Fore.YELLOW)
print()
print("Please Wait, Encrypting File...")
pyAesCrypt.encryptFile(filePath, dest, password, bufferSize)
printSucess("File Encrypted Sucessfully.")
printError("Deleting {0}...".format(source))
os.remove(source)
printSucess("{0} Deleted.".format(source))
print()
changeForeColor(Fore.LIGHTGREEN_EX)
print("Encrypting Complete.".center(30, "-"))
print()
except:
printError("")
printEnterAnyKey()
if select==2:
while not os.path.exists(filePath) or not os.path.isdir(filePath):
changeForeColor(Fore.YELLOW)
print()
print("Enter a valid directory path:")
changeForeColor(Fore.WHITE)
filePath = input()
table = PrettyTable()
table.field_names = ["#", "Name"]
counter = 1
list_of_fad = os.listdir(filePath)
for item in list_of_fad:
current_path = ""
if filePath.endswith("/") or filePath.endswith("\\"):
current_path = filePath + item
else:
current_path = filePath + "/" + item
if os.path.isfile(current_path):
table.add_row([str(counter), os.path.basename(item)])
counter += 1
print(table)
print()
while empty(password):
changeForeColor(Fore.LIGHTBLUE_EX)
password = input("Please Enter Your Password: ")
changeForeColor(Fore.WHITE)
print()
for item in os.listdir(filePath):
current_path = ""
if filePath.endswith("/") or filePath.endswith("\\"):
current_path = filePath + item
else:
current_path = filePath + "/" + item
source = current_path
if os.path.isfile(source):
dest = source + ".aes"
try:
changeForeColor(Fore.YELLOW)
print("Please Wait, Encrypting File...")
pyAesCrypt.encryptFile(current_path, dest, password, bufferSize)
printSucess("{0} Encrypted Sucessfully.".format(item))
printError("Deleting {0}...".format(item))
os.remove(current_path)
printSucess("{0} Deleted.".format(current_path))
print()
except:
printError("")
printEnterAnyKey()
print()
changeForeColor(Fore.LIGHTGREEN_EX)
print("Encrypting Complete.".center(30, "-"))
print()
#-------------------------------------------------------------------------
def decrypt():
clearScreen()
filePath = ""
source = ""
dest = ""
global password
changeForeColor(Fore.YELLOW)
print("Decrypt Files".center(30, "-"))
select = 0
while select<1 or select>2:
changeForeColor(Fore.LIGHTGREEN_EX)
print()
print("1- Decrypt File")
print("2- Decrypt Folder Content (Files)")
print()
changeForeColor(Fore.LIGHTBLUE_EX)
inp = input("Enter Option Number: ")
print()
if inp.isdigit():
select = int(inp)
if select<0 or select>2:
continue
if select==1:
while not os.path.exists(filePath) or not os.path.isfile(filePath) or not filePath.endswith(".aes"):
changeForeColor(Fore.YELLOW)
print("Enter a valid file path:")
changeForeColor(Fore.WHITE)
filePath = input()
fileDir, fileName = os.path.split(filePath)
source = filePath
if fileDir.endswith("/") or fileDir.endswith("\\"):
dest = fileDir + fileName
else:
dest = fileDir + "/" + fileName
if source.endswith(".aes"):
dest = source[0:-4]
while empty(password):
changeForeColor(Fore.LIGHTBLUE_EX)
print()
password = input("Please Enter Your Password: ")
changeForeColor(Fore.WHITE)
try:
changeForeColor(Fore.YELLOW)
print()
print("Please Wait, Decrypting File...")
pyAesCrypt.decryptFile(filePath, dest, password, bufferSize)
printSucess("File Decrypted Sucessfully.")
printError("Deleting {0}...".format(source))
os.remove(source)
printSucess("{0} Deleted.".format(source))
print()
changeForeColor(Fore.LIGHTGREEN_EX)
print("Decrypting Complete.".center(30, "-"))
print()
except:
printError("")
printEnterAnyKey()
if select==2:
while not os.path.exists(filePath):
changeForeColor(Fore.YELLOW)
print()
print("Enter a valid directory path:")
changeForeColor(Fore.WHITE)
filePath = input()
print()
table = PrettyTable()
table.field_names = ["#", "Name"]
counter = 1
aes_counter = 0
list_of_fad = os.listdir(filePath)
for item in list_of_fad:
current_path = ""
if filePath.endswith("/") or filePath.endswith("\\"):
current_path = filePath + item
else:
current_path = filePath + "/" + item
if os.path.isfile(current_path) and filePath.endswith(".aes"):
table.add_row([str(counter), os.path.basename(item)])
counter += 1
aes_counter += 1
print(table)
print()
if aes_counter==0:
print()
printError("Files Not Found...")
return
while empty(password):
changeForeColor(Fore.LIGHTBLUE_EX)
password = input("Please Enter Your Password: ")
changeForeColor(Fore.WHITE)
print()
for item in os.listdir(filePath):
current_path = ""
if filePath.endswith("/") or filePath.endswith("\\"):
current_path = filePath + item
else:
current_path = filePath + "/" + item
source = current_path
if os.path.isfile(source) and filePath.endswith(".aes"):
dest = source[0:-4]
try:
changeForeColor(Fore.YELLOW)
print("Please Wait, Decrypting File...")
pyAesCrypt.decryptFile(current_path, dest, password, bufferSize)
printSucess("{0} Decrypted Sucessfully.".format(item))
printError("Deleting {0}...".format(item))
os.remove(current_path)
printSucess("{0} Deleted.".format(current_path))
print()
except:
printError("")
printEnterAnyKey()
print()
changeForeColor(Fore.LIGHTGREEN_EX)
print("Decrypting Complete.".center(30, "-"))
print()
#-------------------------------------------------------------------------
def aboutMe():
clearScreen()
changeForeColor(Fore.YELLOW)
print("About Me".center(30, "-"))
changeForeColor(Fore.CYAN)
print("Programmer: Javad Moradkhah")
changeForeColor(Fore.LIGHTGREEN_EX)
print("Date: 9 June 2020 | 1399/03/20")
print("".center(30, "-"))
#-------------------------------------------------------------------------
def callFun(n):
if n==1:
encrypt()
elif n==2:
decrypt()
elif n==3:
aboutMe()
elif n==4:
os.system("exit")
#-------------------------------------------------------------------------
select = 0
while select<1 or select>4:
os.system('cls')
print(Fore.LIGHTRED_EX + 'Welcome To LFF (Lock Ficking Files)', file=stream)
print()
print(Fore.LIGHTGREEN_EX + '1- Encrypt Files', file=stream)
print(Fore.LIGHTGREEN_EX + '2- Decrypt Files', file=stream)
print(Fore.LIGHTGREEN_EX + '3- About Me', file=stream)
print(Fore.LIGHTGREEN_EX + '4- Exit', file=stream)
print()
changeForeColor(Fore.LIGHTBLUE_EX)
inp = input("Enter Option Number: ")
if inp.isdigit() and (select>=1 or select<=4):
select = int(inp)
callFun(select)
مطلبی دیگر از این انتشارات
استفاده از آی جی تی وی در کسب و کار
مطلبی دیگر از این انتشارات
سه اشتباهی که برنامه نویسان مبتدی React با state کامپوننت مرتکب میشن
مطلبی دیگر از این انتشارات
۱۰ + ۱ از رمز های باحال برای ماین کرافت