Potřeboval bych pomoct snažim se pro RSA šifrovaní, aby fungovalo misto šifrovaní po znacích tak bude šifrovat bloky, pro jeden blok mi převod funguje, ale pokud se vytvoří další tak převod nefunguje a nevím jak to opravit
def text_to_numeric_binary_blocks(text):
block_size = 10
numeric_decimal_blocks = []
for i in range(0, len(text), block_size):
block = text[i:i + block_size]
numeric_decimal_block = int(''.join(format(ord(char), '010b') for char in block), 2)
numeric_decimal_blocks.append(numeric_decimal_block)
numeric_binary_blocks = [format(block, '010b') for block in numeric_decimal_blocks]
# Join binary blocks into a single string without separators
binary_representation = ''.join(numeric_binary_blocks)
return numeric_decimal_blocks, binary_representation
def numeric_to_text_binary_blocks(numeric_decimal_blocks, block_size):
# Convert each numeric block to a string and then concatenate them
numeric_representation = ''.join(map(str, numeric_decimal_blocks))
# Convert the combined string to an integer
numeric_representation = int(numeric_representation)
# Convert the integer to binary representation
binary_representation = format(numeric_representation, 'b')
# Pad the binary representation to ensure a multiple of block_size for ASCII conversion
binary_representation = binary_representation.rjust(((len(binary_representation) + block_size - 1) // block_size) * block_size, '0')
# Split the binary representation into blocks of size block_size
binary_blocks = [binary_representation[i:i + block_size] for i in range(0, len(binary_representation), block_size)]
# Convert each block back to an integer and then to its ASCII character
text = ''.join(chr(int(block, 2)) for block in binary_blocks)
# Remove any extra null characters at the end
text = text.rstrip('\x00')
return text, numeric_decimal_blocks
def test_conversion(message):
numeric_decimal_blocks, binary_representation = text_to_numeric_binary_blocks(message)
converted_text, _ = numeric_to_text_binary_blocks(numeric_decimal_blocks, 10)
print(f"Original message: {message}")
print(f"Numeric Decimal Blocks: {numeric_decimal_blocks}")
print(f"ASCII Values: {[ord(char) for char in message]}")
print(f"Binary Blocks: {binary_representation}")
print(f"Converted message: {converted_text}")
if __name__ == "__main__":
test_conversion("1234567890")
test_conversion("12345678901")