Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import scrolledtext
- from Crypto.Cipher import ChaCha20_Poly1305
- from base58 import b58encode, b58decode
- def encrypt():
- # Get the input text, key, and nonce
- input_text = input_text_box.get("1.0", tk.END).strip()
- key = key_entry.get().strip().encode('utf-8')
- nonce = nonce_entry.get().strip().encode('utf-8')
- # Validate key and nonce lengths
- if len(key) != 32:
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, "Key must be 32 characters long.")
- return
- if len(nonce) != 12:
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, "Nonce must be 12 characters long.")
- return
- # Create ChaCha20-Poly1305 cipher
- cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce)
- # Encrypt the input text and get the ciphertext and MAC tag
- ciphertext, tag = cipher.encrypt_and_digest(input_text.encode())
- # Combine ciphertext and tag, then encode in Base58
- encrypted = b58encode(ciphertext + tag).decode('utf-8')
- # Display the result
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, encrypted)
- def decrypt():
- # Get the encrypted Base58 text, key, and nonce
- encrypted_base58 = input_text_box.get("1.0", tk.END).strip()
- key = key_entry.get().strip().encode('utf-8')
- nonce = nonce_entry.get().strip().encode('utf-8')
- # Validate key and nonce lengths
- if len(key) != 32:
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, "Key must be 32 characters long.")
- return
- if len(nonce) != 12:
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, "Nonce must be 12 characters long.")
- return
- try:
- # Decode Base58 to get the combined ciphertext and tag
- encrypted_data = b58decode(encrypted_base58)
- # Split the ciphertext and tag
- ciphertext = encrypted_data[:-16]
- tag = encrypted_data[-16:]
- # Create ChaCha20-Poly1305 cipher
- cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce)
- # Decrypt the ciphertext and verify the tag
- decrypted_bytes = cipher.decrypt_and_verify(ciphertext, tag)
- # Convert decrypted bytes to a string
- decrypted_text = decrypted_bytes.decode("utf-8")
- # Display the result
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, decrypted_text)
- except (ValueError, KeyError):
- output_text_box.delete("1.0", tk.END)
- output_text_box.insert(tk.END, "Decryption failed or integrity check failed.")
- # Create the main window
- window = tk.Tk()
- window.title("ChaCha20-Poly1305 Encryption/Decryption Tool with Base58")
- # Create key entry field
- key_label = tk.Label(window, text="Key (32 characters):")
- key_label.pack()
- key_entry = tk.Entry(window, width=50)
- key_entry.pack()
- # Create nonce entry field
- nonce_label = tk.Label(window, text="Nonce (12 characters):")
- nonce_label.pack()
- nonce_entry = tk.Entry(window, width=50)
- nonce_entry.pack()
- # Create input text box for plain text or encrypted Base58
- input_label = tk.Label(window, text="Input Text (or Encrypted Base58):")
- input_label.pack()
- input_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
- input_text_box.pack()
- # Create Encrypt button
- encrypt_button = tk.Button(window, text="Encrypt", command=encrypt)
- encrypt_button.pack()
- # Create Decrypt button
- decrypt_button = tk.Button(window, text="Decrypt", command=decrypt)
- decrypt_button.pack()
- # Create output text box for result
- output_label = tk.Label(window, text="Output Text:")
- output_label.pack()
- output_text_box = scrolledtext.ScrolledText(window, width=50, height=10)
- output_text_box.pack()
- # Start the Tkinter event loop
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement