I need help modifying the code to work for size 10, block_size=10. For size 8, block_size=8 works correctly if I have block_size=8
I tried to change the size to 10, but the problem is that in:lower_limit = 10 ** 11,upper_limit = 10 ** 12, the size sometimes does not fit and the code is generated incorrectly.
I would like it to work for block_size=10, but the length must be lower_limit = 10 ** 11,upper_limit = 10 ** 12
![[Image: 1.PNG?ex=6575ac42&is=65633742&hm=a4d5b99...ormat=webp]](https://media.discordapp.net/attachments/805041073768366080/1178308451449962576/1.PNG?ex=6575ac42&is=65633742&hm=a4d5b993e2a474ee31a89215246aaff89b36e54893b2bf31eda53219445fa918&=&format=webp)
I tried to change the size to 10, but the problem is that in:lower_limit = 10 ** 11,upper_limit = 10 ** 12, the size sometimes does not fit and the code is generated incorrectly.
I would like it to work for block_size=10, but the length must be lower_limit = 10 ** 11,upper_limit = 10 ** 12
import random
from math import gcd
from sympy import mod_inverse, isprime
class SimpleRSA:
def __init__(self):
self.public_key = None
self.private_key = None
def generate_keypair(self):
if self.public_key is not None and self.private_key is not None:
return
p = self.generate_large_prime()
q = self.generate_large_prime()
n = p * q
phi = (p - 1) * (q - 1)
e = random.randint(2, phi - 1)
while gcd(e, phi) != 1:
e = random.randint(2, phi - 1)
d = mod_inverse(e, phi)
self.public_key = (n, e)
self.private_key = (n, d)
print(f"p: {p}")
print(f"q: {q}")
print(f"n: {n}")
return self.public_key, self.private_key
def generate_large_prime(self):
lower_limit = 10 ** 11
upper_limit = 10 ** 12
potential_prime = random.randint(lower_limit, upper_limit)
while not isprime(potential_prime):
potential_prime = random.randint(lower_limit, upper_limit)
return potential_prime
def encrypt(self, message):
n, e = self.public_key
binary_blocks = self.text_to_numeric_binary_blocks(message)
print(f"binary_blocks: {binary_blocks}")
encrypted_blocks = [pow(int(block, 2), e, n) for block in binary_blocks]
print(f"encrypted_blocks: {encrypted_blocks}")
return encrypted_blocks
def decrypt(self, encrypted_blocks):
n, d = self.private_key
decrypted_blocks = [format(pow(block, d, n), '0b') for block in encrypted_blocks]
print(f"decrypted_blocks: {decrypted_blocks}")
decrypted_message = self.numeric_to_text_binary_blocks(decrypted_blocks)
print(f"decrypted_message: {decrypted_message}")
return decrypted_message
def text_to_numeric_binary_blocks(self, text, block_size=10):
text = text.encode()
binary_blocks = [
''.join(format(byte, '08b') for byte in text[i:i + block_size]).ljust(block_size * 8, '0')
for i in range(0, len(text), block_size)
]
return binary_blocks
def numeric_to_text_binary_blocks(self, binary_blocks, block_size=10):
bytes_list = [int(block, 2).to_bytes(block_size, 'big') for block in binary_blocks]
text = b''.join(bytes_list).decode(errors='replace').rstrip("\x00")
return text
def test_encryption_decryption(message):
rsa = SimpleRSA()
public_key, private_key = rsa.generate_keypair()
print(f"Original message: {message}")
# Encryption
encrypted_blocks = rsa.encrypt(message)
print(f"Encrypted blocks: {encrypted_blocks}")
# Decryption
decrypted_message = rsa.decrypt(encrypted_blocks)
print(f"Decrypted message: {decrypted_message}")
if __name__ == "__main__":
test_encryption_decryption("kolotoč")