Advertisement
Guest User

Untitled

a guest
Aug 30th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import scrolledtext
  3. from Crypto.Cipher import AES
  4. from Crypto.Util.Padding import pad, unpad
  5.  
  6. # Custom Base58 character set (Bitcoin Base58 alphabet)
  7. BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  8.  
  9. def base58_encode(data):
  10. """Encodes bytes to Base58."""
  11. num = int.from_bytes(data, "big")
  12. if num == 0:
  13. return BASE58_ALPHABET[0]
  14.  
  15. encoded = []
  16. base = len(BASE58_ALPHABET)
  17. while num:
  18. num, rem = divmod(num, base)
  19. encoded.append(BASE58_ALPHABET[rem])
  20. return ''.join(reversed(encoded))
  21.  
  22. def base58_decode(data):
  23. """Decodes Base58 string to bytes."""
  24. base = len(BASE58_ALPHABET)
  25. num = 0
  26. for char in data:
  27. num = num * base + BASE58_ALPHABET.index(char)
  28. return num.to_bytes((num.bit_length() + 7) // 8, 'big') or b'\0'
  29.  
  30. def encrypt():
  31. # Get the input text
  32. input_text = input_text_box.get("1.0", tk.END).strip()
  33.  
  34. # Define the predefined key and IV as bytes
  35. key = bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000") # 32 bytes for AES-256
  36. iv = bytes.fromhex("00000000000000000000000000000000") # 16 bytes for 128-bit IV
  37.  
  38. # Create AES cipher in CBC mode with AES-256
  39. cipher = AES.new(key, AES.MODE_CBC, iv)
  40.  
  41. # Encrypt the input text, ensuring it's padded to the block size
  42. encrypted_bytes = cipher.encrypt(pad(input_text.encode(), AES.block_size))
  43.  
  44. # Convert encrypted bytes to Base58
  45. encrypted_base58 = base58_encode(encrypted_bytes)
  46.  
  47. # Display the result
  48. output_text_box.delete("1.0", tk.END)
  49. output_text_box.insert(tk.END, encrypted_base58)
  50.  
  51. def decrypt():
  52. # Get the encrypted Base58 text
  53. encrypted_base58 = input_text_box.get("1.0", tk.END).strip()
  54.  
  55. # Define the predefined key and IV as bytes
  56. key = bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000") # 32 bytes for AES-256
  57. iv = bytes.fromhex("00000000000000000000000000000000") # 16 bytes for 128-bit IV
  58.  
  59. # Decode Base58 to get the encrypted bytes
  60. encrypted_bytes = base58_decode(encrypted_base58)
  61.  
  62. # Create AES cipher in CBC mode with AES-256
  63. cipher = AES.new(key, AES.MODE_CBC, iv)
  64.  
  65. # Decrypt the text and remove padding
  66. decrypted_bytes = unpad(cipher.decrypt(encrypted_bytes), AES.block_size)
  67.  
  68. # Convert decrypted bytes to a string
  69. decrypted_text = decrypted_bytes.decode("utf-8")
  70.  
  71. # Display the result
  72. output_text_box.delete("1.0", tk.END)
  73. output_text_box.insert(tk.END, decrypted_text)
  74.  
  75. # Create the main window
  76. window = tk.Tk()
  77. window.title("AES-256 Encryption/Decryption Tool with Predefined Key and IV (Base58)")
  78.  
  79. # Create input text box for plain text or encrypted Base58
  80. input_label = tk.Label(window, text="Input Text (or Encrypted Base58):")
  81. input_label.pack()
  82. input_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
  83. input_text_box.pack()
  84.  
  85. # Create Encrypt button
  86. encrypt_button = tk.Button(window, text="Encrypt", command=encrypt)
  87. encrypt_button.pack()
  88.  
  89. # Create Decrypt button
  90. decrypt_button = tk.Button(window, text="Decrypt", command=decrypt)
  91. decrypt_button.pack()
  92.  
  93. # Create output text box for result
  94. output_label = tk.Label(window, text="Output Text:")
  95. output_label.pack()
  96. output_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
  97. output_text_box.pack()
  98.  
  99. # Start the Tkinter event loop
  100. window.mainloop()
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement