DrAungWinHtut

AESwithGUI.py

Dec 2nd, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog, messagebox
  4. from cryptography.hazmat.backends import default_backend
  5. from cryptography.hazmat.primitives import hashes, padding
  6. from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
  7. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  8. from base64 import urlsafe_b64encode, urlsafe_b64decode, b64encode, b64decode
  9.  
  10.  
  11. # Function to derive a key from a password
  12. def derive_key(password: str):
  13.     password = password.encode()
  14.     kdf = PBKDF2HMAC(
  15.         algorithm=hashes.SHA256(),
  16.         length=32,
  17.         salt=b'',  # Empty salt as per requirement
  18.         iterations=100000,
  19.         backend=default_backend()
  20.     )
  21.     return kdf.derive(password)
  22.  
  23. # AES Encryption
  24. def pad(s):
  25.     return s + b"=" * ((4 - len(s) % 4) % 4)
  26.  
  27. def encrypt_aes(key, plaintext):
  28.     padder = padding.PKCS7(128).padder()
  29.     padded_data = padder.update(plaintext.encode()) + padder.finalize()
  30.  
  31.     iv = os.urandom(16)
  32.     cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
  33.     encryptor = cipher.encryptor()
  34.  
  35.     ciphertext = encryptor.update(padded_data) + encryptor.finalize()
  36.  
  37.     encoded = urlsafe_b64encode(iv + ciphertext)
  38.     return encoded.decode()
  39.  
  40. def decrypt_aes(key, ciphertext):
  41.     try:
  42.         decoded_ciphertext = urlsafe_b64decode(ciphertext)
  43.     except (binascii.Error, ValueError) as e:
  44.         # Handle incorrect padding
  45.         raise ValueError("Decoding failed: Incorrect padding or corrupted data") from e
  46.  
  47.     iv = decoded_ciphertext[:16]
  48.     ciphertext = decoded_ciphertext[16:]
  49.  
  50.     cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
  51.     decryptor = cipher.decryptor()
  52.  
  53.     padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
  54.     unpadder = padding.PKCS7(128).unpadder()
  55.  
  56.     return (unpadder.update(padded_plaintext) + unpadder.finalize()).decode()
  57.  
  58.  
  59. # Updated file encryption and decryption functions
  60. def encrypt_files_in_folder(folder_path, key):
  61.     for filename in os.listdir(folder_path):
  62.         file_path = os.path.join(folder_path, filename)
  63.         if os.path.isfile(file_path):
  64.             with open(file_path, 'rb') as file:
  65.                 file_data = file.read()
  66.             encrypted_data = encrypt_aes(key, file_data.decode('utf-8', 'ignore'))
  67.             with open(file_path, 'w') as file:
  68.                 file.write(encrypted_data)
  69.  
  70. def decrypt_files_in_folder(folder_path, key):
  71.     for filename in os.listdir(folder_path):
  72.         file_path = os.path.join(folder_path, filename)
  73.         if os.path.isfile(file_path):
  74.             with open(file_path, 'r') as file:
  75.                 file_data = file.read()
  76.             decrypted_data = decrypt_aes(key, file_data)
  77.             with open(file_path, 'wb') as file:
  78.                 file.write(decrypted_data.encode('utf-8'))
  79.  
  80.  
  81. # GUI Functions
  82. def browse_folder():
  83.     folder_path = filedialog.askdirectory()
  84.     if folder_path:
  85.         folder_path_entry.delete(0, tk.END)
  86.         folder_path_entry.insert(0, folder_path)
  87.  
  88.  
  89.  
  90.  
  91.  
  92. # Updated GUI Functions
  93. def encrypt_folder():
  94.     folder_path = folder_path_entry.get()
  95.     password = password_entry.get()
  96.     if not folder_path or not password:
  97.         messagebox.showerror("Error", "Folder path and password are required.")
  98.         return
  99.  
  100.     key = derive_key(password)
  101.     encrypt_files_in_folder(folder_path, key)
  102.  
  103.     messagebox.showinfo("Success", "Folder encrypted successfully.")
  104.  
  105. def decrypt_folder():
  106.     folder_path = folder_path_entry.get()
  107.     password = password_entry.get()
  108.     if not folder_path or not password:
  109.         messagebox.showerror("Error", "Folder path and password are required.")
  110.         return
  111.  
  112.     key = derive_key(password)
  113.     decrypt_files_in_folder(folder_path, key)
  114.  
  115.     messagebox.showinfo("Success", "Folder decrypted successfully.")
  116.  
  117. def encrypt_text():
  118.     text = text_entry.get()
  119.     password = password_entry.get()
  120.     if not text or not password:
  121.         messagebox.showerror("Error", "Text and password are required.")
  122.         return
  123.  
  124.     key = derive_key(password)
  125.     encrypted_text = encrypt_aes(key, text)
  126.  
  127.     result_text.delete(0, tk.END)
  128.     result_text.insert(0, encrypted_text)
  129.  
  130. def decrypt_text():
  131.     encrypted_text = text_entry.get()
  132.     password = password_entry.get()
  133.     if not encrypted_text or not password:
  134.         messagebox.showerror("Error", "Encrypted text and password are required.")
  135.         return
  136.  
  137.     key = derive_key(password)
  138.     decrypted_text = decrypt_aes(key, encrypted_text)
  139.  
  140.     result_text.delete(0, tk.END)
  141.     result_text.insert(0, decrypted_text)
  142.  
  143. # GUI Layout
  144. main_window = tk.Tk()
  145. main_window.title("AES Encryption/Decryption")
  146.  
  147. window = tk.Frame(main_window, padx=15, pady=15)
  148. window.grid(row=6, column=0, columnspan=3, sticky="ew")
  149.  
  150. tk.Label(window, padx=5, pady=5, text="Folder Path:").grid(row=0, column=0,sticky='w')
  151. folder_path_entry = tk.Entry(window, width=50)
  152. folder_path_entry.grid(row=0, column=1)
  153.  
  154. browse_frame = tk.Frame(window,padx=5, pady=5,)
  155. browse_frame.grid(row=0, column=3, sticky="ew")
  156. tk.Button(browse_frame,padx=15, pady=5, text="Browse", command=browse_folder).grid(row=0, column=2)
  157.  
  158. folder_ops_frame = tk.Frame(window,padx=5, pady=5,)
  159. folder_ops_frame.grid(row=1, column=1, sticky="ew")
  160.  
  161. # Center the buttons in the frame and space them equally
  162. tk.Button(folder_ops_frame, text="Encrypt Folder", command=encrypt_folder).pack(side=tk.LEFT, expand=True, fill=tk.X, padx=5)
  163. tk.Button(folder_ops_frame, text="Decrypt Folder", command=decrypt_folder).pack(side=tk.LEFT, expand=True, fill=tk.X, padx=5)
  164.  
  165.  
  166. tk.Label(window,padx=5, pady=5, text="Text:").grid(row=2, column=0,sticky='w')
  167. text_entry = tk.Entry(window, width=50)
  168. text_entry.grid(row=2, column=1)
  169.  
  170. file_ops_frame = tk.Frame(window,padx=5, pady=5,)
  171. file_ops_frame.grid(row=3, column=1, sticky="ew")
  172.  
  173. # Center the buttons in the frame and space them equally
  174. tk.Button(file_ops_frame, text="Encrypt Text", command=encrypt_text).pack(side=tk.LEFT, expand=True, fill=tk.X, padx=5)
  175. tk.Button(file_ops_frame, text="Decrypt Text", command=decrypt_text).pack(side=tk.LEFT, expand=True, fill=tk.X, padx=5)
  176.  
  177.  
  178. tk.Label(window,padx=5, pady=5, text="Password:").grid(row=4, column=0,sticky='w')
  179. password_entry = tk.Entry(window, show="*", width=50)
  180. password_entry.grid(row=4, column=1)
  181.  
  182. tk.Label(window,padx=5, pady=5, text="Result:").grid(row=5, column=0,sticky='w')
  183. result_text = tk.Entry(window, width=50)
  184. result_text.grid(row=5, column=1)
  185.  
  186. window.mainloop()
  187.  
  188.  
Add Comment
Please, Sign In to add comment