سورس برنامه رمزنگاری و رمزگشایی فایل به زبان پایتون

امروز سورس برنامه رمزنگاری و رمزگشایی فایل ها رو که به زبان پایتون نوشتم رو در اختیارتون میذارم. امید وارم براتون مفید باشه.

توجه: برای استفاده از سورس باید سه کتابخونه: 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 = ''&quot
#-------------------------------------------------------------------------
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(&quotDo you want to continue? (yes/no) &quot)
    if not res == 'yes':
    os.system(&quotexit&quot)
#-------------------------------------------------------------------------
def printText(msg):
    changeForeColor(Fore.WHITE)
    print(msg)

def printError(error):
    changeForeColor(Fore.RED)
    if  not error:
        print(&quotAn Error Occurred!&quot)
    else:
        print(error)

def printSucess(msg):
    changeForeColor(Fore.GREEN)
    print(msg)
#-------------------------------------------------------------------------
def getDesktopPath():
    return (&quotC:\\&quot + os.path.join(os.environ[&quotHOMEPATH&quot], &quotDesktop&quot))
#-------------------------------------------------------------------------
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, &quotrb&quot).read()
#-------------------------------------------------------------------------
def encrypt():
    clearScreen()
    filePath = &quot&quot
    source = &quot&quot
    dest = &quot&quot
    global password
    changeForeColor(Fore.YELLOW)
    print(&quotEncrypt Files&quot.center(30, &quot-&quot))
    select = 0
    while select<1 or select>2:
        changeForeColor(Fore.LIGHTGREEN_EX)
        print()
        print(&quot1- Encrypt File&quot)
        print(&quot2- Encrypt Folder Content (Files)&quot)
        print()
        changeForeColor(Fore.LIGHTBLUE_EX)
        inp = input(&quotEnter Option Number: &quot)
        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(&quotEnter a valid file path:&quot)
            changeForeColor(Fore.WHITE)
            filePath = input()

        fileDir, fileName = os.path.split(filePath)
        source = filePath

        if fileDir.endswith(&quot/&quot) or fileDir.endswith(&quot\\&quot):
            dest = fileDir + fileName + &quot.aes&quot
        else:
            dest = fileDir + &quot/&quot + fileName + &quot.aes&quot

    while empty(password):
        changeForeColor(Fore.LIGHTBLUE_EX)
        print()
        password = input(&quotPlease Enter Your Password: &quot)
        changeForeColor(Fore.WHITE)

    try:
        changeForeColor(Fore.YELLOW)
        print()
        print(&quotPlease Wait, Encrypting File...&quot)
        pyAesCrypt.encryptFile(filePath, dest, password, bufferSize)
        printSucess(&quotFile Encrypted Sucessfully.&quot)
        printError(&quotDeleting {0}...&quot.format(source))
        os.remove(source)
        printSucess(&quot{0} Deleted.&quot.format(source))
        print()
        changeForeColor(Fore.LIGHTGREEN_EX)
        print(&quotEncrypting Complete.&quot.center(30, &quot-&quot))
        print()
    except:
        printError(&quot&quot)
    printEnterAnyKey()

    if select==2:
        while not os.path.exists(filePath) or not os.path.isdir(filePath):
            changeForeColor(Fore.YELLOW)
            print()
            print(&quotEnter a valid directory path:&quot)
            changeForeColor(Fore.WHITE)
            filePath = input()
            table = PrettyTable()
            table.field_names = [&quot#&quot, &quotName&quot]
            counter = 1
            list_of_fad = os.listdir(filePath)
            for item in list_of_fad:
                current_path = &quot&quot
                if filePath.endswith(&quot/&quot) or filePath.endswith(&quot\\&quot):
                    current_path = filePath + item
                else:
                    current_path = filePath + &quot/&quot + 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(&quotPlease Enter Your Password: &quot)
            changeForeColor(Fore.WHITE)
            print()

    for item in os.listdir(filePath):
        current_path = &quot&quot
        if filePath.endswith(&quot/&quot) or filePath.endswith(&quot\\&quot):
            current_path = filePath + item
        else:
            current_path = filePath + &quot/&quot + item
        source = current_path
        if os.path.isfile(source):
            dest = source + &quot.aes&quot
        try:
            changeForeColor(Fore.YELLOW)
            print(&quotPlease Wait, Encrypting File...&quot)
            pyAesCrypt.encryptFile(current_path, dest, password, bufferSize)
            printSucess(&quot{0} Encrypted Sucessfully.&quot.format(item))
            printError(&quotDeleting {0}...&quot.format(item))
            os.remove(current_path)
            printSucess(&quot{0} Deleted.&quot.format(current_path))
            print()
        except:
            printError(&quot&quot)
            printEnterAnyKey()
            print()
        changeForeColor(Fore.LIGHTGREEN_EX)
        print(&quotEncrypting Complete.&quot.center(30, &quot-&quot))
        print()

#-------------------------------------------------------------------------
def decrypt():
    clearScreen()
    filePath = &quot&quot
    source = &quot&quot
    dest = &quot&quot
    global password
    changeForeColor(Fore.YELLOW)
    print(&quotDecrypt Files&quot.center(30, &quot-&quot))
    select = 0
    while select<1 or select>2:
    changeForeColor(Fore.LIGHTGREEN_EX)
    print()
    print(&quot1- Decrypt File&quot)
    print(&quot2- Decrypt Folder Content (Files)&quot)
    print()
    changeForeColor(Fore.LIGHTBLUE_EX)
    inp = input(&quotEnter Option Number: &quot)
    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(&quot.aes&quot):
        changeForeColor(Fore.YELLOW)
        print(&quotEnter a valid file path:&quot)
        changeForeColor(Fore.WHITE)
        filePath = input()
        fileDir, fileName = os.path.split(filePath)
        source = filePath

        if fileDir.endswith(&quot/&quot) or fileDir.endswith(&quot\\&quot):
            dest = fileDir + fileName
        else:
            dest = fileDir + &quot/&quot + fileName

        if source.endswith(&quot.aes&quot):
            dest = source[0:-4]

        while empty(password):
            changeForeColor(Fore.LIGHTBLUE_EX)
            print()
            password = input(&quotPlease Enter Your Password: &quot)
            changeForeColor(Fore.WHITE)

            try:
                changeForeColor(Fore.YELLOW)
                print()
                print(&quotPlease Wait, Decrypting File...&quot)
                pyAesCrypt.decryptFile(filePath, dest, password, bufferSize)
                printSucess(&quotFile Decrypted Sucessfully.&quot)
                printError(&quotDeleting {0}...&quot.format(source))
                os.remove(source)
                printSucess(&quot{0} Deleted.&quot.format(source))
                print()
                changeForeColor(Fore.LIGHTGREEN_EX)
                print(&quotDecrypting Complete.&quot.center(30, &quot-&quot))
                print()
            except:
                printError(&quot&quot)
                printEnterAnyKey()
        if select==2:
            while not os.path.exists(filePath):
                 changeForeColor(Fore.YELLOW)
                 print()
                 print(&quotEnter a valid directory path:&quot)
                 changeForeColor(Fore.WHITE)
                 filePath = input()
                 print()

    table = PrettyTable()
    table.field_names = [&quot#&quot, &quotName&quot]
    counter = 1
    aes_counter = 0
    list_of_fad = os.listdir(filePath)
    for item in list_of_fad:
        current_path = &quot&quot
        if filePath.endswith(&quot/&quot) or filePath.endswith(&quot\\&quot):
            current_path = filePath + item
        else:
            current_path = filePath + &quot/&quot + item
            if os.path.isfile(current_path) and filePath.endswith(&quot.aes&quot):
                table.add_row([str(counter), os.path.basename(item)])
                counter += 1
                aes_counter += 1
        print(table)
        print()

        if aes_counter==0:
            print()
            printError(&quotFiles Not Found...&quot)
            return

        while empty(password):
            changeForeColor(Fore.LIGHTBLUE_EX)
            password = input(&quotPlease Enter Your Password: &quot)
            changeForeColor(Fore.WHITE)
            print()

        for item in os.listdir(filePath):
            current_path = &quot&quot

        if filePath.endswith(&quot/&quot) or filePath.endswith(&quot\\&quot):
            current_path = filePath + item
        else:
            current_path = filePath + &quot/&quot + item

         source = current_path
        if os.path.isfile(source) and filePath.endswith(&quot.aes&quot):
            dest = source[0:-4]
        try:
            changeForeColor(Fore.YELLOW)
            print(&quotPlease Wait, Decrypting File...&quot)
            pyAesCrypt.decryptFile(current_path, dest, password, bufferSize)
            printSucess(&quot{0} Decrypted Sucessfully.&quot.format(item))
            printError(&quotDeleting {0}...&quot.format(item))
            os.remove(current_path)
            printSucess(&quot{0} Deleted.&quot.format(current_path))
            print()
        except:
            printError(&quot&quot)
            printEnterAnyKey()
            print()

    changeForeColor(Fore.LIGHTGREEN_EX)
    print(&quotDecrypting Complete.&quot.center(30, &quot-&quot))
    print()
#-------------------------------------------------------------------------
def aboutMe():
    clearScreen()
    changeForeColor(Fore.YELLOW)
    print(&quotAbout Me&quot.center(30, &quot-&quot))
    changeForeColor(Fore.CYAN)
    print(&quotProgrammer: Javad Moradkhah&quot)
    changeForeColor(Fore.LIGHTGREEN_EX)
    print(&quotDate: 9 June 2020 | 1399/03/20&quot)
    print(&quot&quot.center(30, &quot-&quot))

#-------------------------------------------------------------------------
def callFun(n):
    if n==1:
        encrypt()
    elif n==2:
        decrypt()
    elif n==3:
        aboutMe()
    elif n==4:
        os.system(&quotexit&quot)

#-------------------------------------------------------------------------
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(&quotEnter Option Number: &quot)
    if inp.isdigit() and (select>=1 or select<=4):
        select = int(inp)
        callFun(select)

موفق باشید