Шифрование и дешифрование с использованием PythonPython

Программы на Python
Anonymous
Шифрование и дешифрование с использованием Python

Сообщение Anonymous »

Я должен зашифровать текст с помощью кода шифрования, затем расшифровать его с помощью кода расшифровки и получить исходный текст. однако с моим кодом что-то не так, потому что при попытке расшифровки я получаю разный текст. пожалуйста, помогите, ребята.
кстати, это алгоритм, который мы разработали для задания, а не что-то уже существующее.
Это код моего алгоритма шифрования:

Код: Выделить всё

import binascii
import numpy as np
import tkinter as tk

# Helper Functions for Conversion
def text_to_hex(text):
"""Convert plain text to hexadecimal."""
return binascii.hexlify(text.encode()).decode()

def hex_to_binary(hex_string):
"""Convert hexadecimal to binary."""
return bin(int(hex_string, 16))[2:].zfill(len(hex_string) * 4)

# Constants
NUM_ROUNDS = 16
BLOCK_SIZE = 256
HALF_BLOCK_SIZE = 128
NUM_SUBKEYS = 36

# Helper Functions
def rotate_left(value, shift, size=8):
"""Rotate bits to the left."""
return ((value > (size - shift))

def rotate_right(value, shift, size=8):
"""Rotate bits to the right."""
return (value >> shift) | ((value  32:
plaintext = plaintext[:32]
elif len(plaintext) <  32:
plaintext = plaintext.ljust(32, '\0')  # pad with null bytes

# Convert plaintext to hexadecimal
plaintext_hex = text_to_hex(plaintext)

# Convert hex to bytes
plaintext_bytes = bytes.fromhex(plaintext_hex)

# Define the master key (can be changed)
master_key = "attack_on_titans_AOS_attack_on_titans_AOS".encode()  # 256-bit master key

# Encryption
ciphertext = encrypt_block(plaintext_bytes, master_key)

# Display ciphertext in hexadecimal format
entry_ciphertext.delete(0, tk.END)
entry_ciphertext.insert(0, ciphertext.hex())

# Create the main window
root = tk.Tk()
root.title("Encrypt Text")

# Create and place the components in the window
label_plaintext = tk.Label(root, text="Plaintext:")
label_plaintext.grid(row=0, column=0, padx=10, pady=10)
entry_plaintext = tk.Entry(root, width=40)
entry_plaintext.grid(row=0, column=1, padx=10, pady=10)

label_ciphertext = tk.Label(root, text="Ciphertext:")
label_ciphertext.grid(row=1, column=0, padx=10, pady=10)
entry_ciphertext = tk.Entry(root, width=40)
entry_ciphertext.grid(row=1, column=1, padx=10, pady=10)

button_encrypt = tk.Button(root, text="Encrypt", command=encrypt_text)
button_encrypt.grid(row=2, column=0, columnspan=2, padx=10, pady=10)

# Run the GUI event loop
root.mainloop()

а это код моего алгоритма дешифрования:

Код: Выделить всё

import binascii
import numpy as np
import tkinter as tk

# Helper Functions for Conversion
def text_to_hex(text):
"""Convert plain text to hexadecimal."""
return binascii.hexlify(text.encode()).decode()

def hex_to_binary(hex_string):
"""Convert hexadecimal to binary."""
return bin(int(hex_string, 16))[2:].zfill(len(hex_string) * 4)

# Constants
NUM_ROUNDS = 16
BLOCK_SIZE = 256
HALF_BLOCK_SIZE = 128
NUM_SUBKEYS = 36

# Helper Functions
def rotate_left(value, shift, size=8):
"""Rotate bits to the left."""
return ((value > (size - shift))

def rotate_right(value, shift, size=8):
"""Rotate bits to the right."""
return (value >> shift) | ((value 

Подробнее здесь: [url]https://stackoverflow.com/questions/78813395/encryption-and-decryption-using-python[/url]

Вернуться в «Python»