Advertisement
Guest User

Untitled

a guest
Sep 1st, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import scrolledtext
  3. from Crypto.Cipher import ChaCha20_Poly1305
  4. from base58 import b58encode, b58decode
  5.  
  6. def encrypt():
  7. # Get the input text, key, and nonce
  8. input_text = input_text_box.get("1.0", tk.END).strip()
  9. key = key_entry.get().strip().encode('utf-8')
  10. nonce = nonce_entry.get().strip().encode('utf-8')
  11.  
  12. # Validate key and nonce lengths
  13. if len(key) != 32:
  14. output_text_box.delete("1.0", tk.END)
  15. output_text_box.insert(tk.END, "Key must be 32 characters long.")
  16. return
  17. if len(nonce) != 12:
  18. output_text_box.delete("1.0", tk.END)
  19. output_text_box.insert(tk.END, "Nonce must be 12 characters long.")
  20. return
  21.  
  22. # Create ChaCha20-Poly1305 cipher
  23. cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce)
  24.  
  25. # Encrypt the input text and get the ciphertext and MAC tag
  26. ciphertext, tag = cipher.encrypt_and_digest(input_text.encode())
  27.  
  28. # Combine ciphertext and tag, then encode in Base58
  29. encrypted = b58encode(ciphertext + tag).decode('utf-8')
  30.  
  31. # Display the result
  32. output_text_box.delete("1.0", tk.END)
  33. output_text_box.insert(tk.END, encrypted)
  34.  
  35. def decrypt():
  36. # Get the encrypted Base58 text, key, and nonce
  37. encrypted_base58 = input_text_box.get("1.0", tk.END).strip()
  38. key = key_entry.get().strip().encode('utf-8')
  39. nonce = nonce_entry.get().strip().encode('utf-8')
  40.  
  41. # Validate key and nonce lengths
  42. if len(key) != 32:
  43. output_text_box.delete("1.0", tk.END)
  44. output_text_box.insert(tk.END, "Key must be 32 characters long.")
  45. return
  46. if len(nonce) != 12:
  47. output_text_box.delete("1.0", tk.END)
  48. output_text_box.insert(tk.END, "Nonce must be 12 characters long.")
  49. return
  50.  
  51. try:
  52. # Decode Base58 to get the combined ciphertext and tag
  53. encrypted_data = b58decode(encrypted_base58)
  54.  
  55. # Split the ciphertext and tag
  56. ciphertext = encrypted_data[:-16]
  57. tag = encrypted_data[-16:]
  58.  
  59. # Create ChaCha20-Poly1305 cipher
  60. cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce)
  61.  
  62. # Decrypt the ciphertext and verify the tag
  63. decrypted_bytes = cipher.decrypt_and_verify(ciphertext, tag)
  64.  
  65. # Convert decrypted bytes to a string
  66. decrypted_text = decrypted_bytes.decode("utf-8")
  67.  
  68. # Display the result
  69. output_text_box.delete("1.0", tk.END)
  70. output_text_box.insert(tk.END, decrypted_text)
  71. except (ValueError, KeyError):
  72. output_text_box.delete("1.0", tk.END)
  73. output_text_box.insert(tk.END, "Decryption failed or integrity check failed.")
  74.  
  75. # Create the main window
  76. window = tk.Tk()
  77. window.title("ChaCha20-Poly1305 Encryption/Decryption Tool with Base58")
  78.  
  79. # Create key entry field
  80. key_label = tk.Label(window, text="Key (32 characters):")
  81. key_label.pack()
  82. key_entry = tk.Entry(window, width=50)
  83. key_entry.pack()
  84.  
  85. # Create nonce entry field
  86. nonce_label = tk.Label(window, text="Nonce (12 characters):")
  87. nonce_label.pack()
  88. nonce_entry = tk.Entry(window, width=50)
  89. nonce_entry.pack()
  90.  
  91. # Create input text box for plain text or encrypted Base58
  92. input_label = tk.Label(window, text="Input Text (or Encrypted Base58):")
  93. input_label.pack()
  94. input_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
  95. input_text_box.pack()
  96.  
  97. # Create Encrypt button
  98. encrypt_button = tk.Button(window, text="Encrypt", command=encrypt)
  99. encrypt_button.pack()
  100.  
  101. # Create Decrypt button
  102. decrypt_button = tk.Button(window, text="Decrypt", command=decrypt)
  103. decrypt_button.pack()
  104.  
  105. # Create output text box for result
  106. output_label = tk.Label(window, text="Output Text:")
  107. output_label.pack()
  108. output_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
  109. output_text_box.pack()
  110.  
  111. # Start the Tkinter event loop
  112. window.mainloop()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement