I would like to make a Python code for Paillier Encryption and I have made the code, which works well with small number e.g. 32bit. But I would like to make it work even though the number is very very large (till 1024 bit)
So I tried this code which p,q (random choosed Primenumber) here is the code
from random import randint
import libnum
import sys
import random
import numpy as np ## in order to save the time
def gcd(a,b):
"""Compute the greatest common divisor of a and b"""
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
"""Compute the lowest common multiple of a and b"""
return a * b // gcd(a, b)
def L(x,n):
return ((x-1)//n)
#%% Key generation (Schlüsselerzeugung)
find_primenumber=2**513
# um die
a = [False,False] + [True]*(find_primenumber-1)
primes=[]
# Algorism of eratosthenes
for i in range(2,find_primenumber+1):
if a[i]:
primes.append(i)
for j in range(2*i, find_primenumber+1, i):
a[j] = False
# print('the choosable prime numbers are : ',primes)
p= random.sample(primes, 2)[0]
q=random.sample(primes, 2)[1] ## two prime numbers are randomly choosed
# öffentliche Schlüssel public key (n,g)
n = p*q
# um Schlüssellänge zu vergrößen, muss n noch größer werden
# sys.getsizeof(n)
# in case n = 2499995600000711 size of n is 32
g = n+1 #Pick a random integer g in the set Z∗n2 (integers between 1 and n2)
gLambda = lcm(p-1,q-1) ## in Matlab cipher = lambda
m= str(input('Enter plaintext :')) #must be string type
m= list(m)
m_list=[]
for i in range(0,len(m)):
m_list.append(ord(m[i])) ## must be saved as a list
m_1=np.array(m_list)
if (len(sys.argv)>1):
m=int(sys.argv[1])
if (len(sys.argv)>2):
p=int(sys.argv[2])
if (len(sys.argv)>3):
q=int(sys.argv[3])
if (p==q):
print("P and Q cannot be the same")
sys.exit()
l = (pow(g, gLambda, n*n)-1)//n
gMu = libnum.invmod(l, n)
if (gcd(g,n*n)==1):
print("g is relatively prime to n*n")
else:
print("WARNING: g is NOT relatively prime to n*n. Will not work!!!")
# geheime Schlüssel (private key)
r=[]
for i in range(0,len(m)):
r.append(randint(1,n))
#%% Encryption
# changed: pow(a,b,n) for modular exponentiation a^b mod n
# --> much faster than the standard pow()
# shortened the code
c=[]
for i in range(0,len(m)):
k1 = pow(g, m_list[i], n**2)
k2 = pow(r[i], n, n**2)
c.append((k1*k2)%(n**2));
but I get this answer from the Console
OverflowError: cannot fit 'int' into an index-sized integer
How can I fix it and make it work even the find_primenumber is large enough?