HeshamTB
611a6c45ea
- Now can generate a 4096-bit key in seconds. However, a longer key makes the cipher way too long. Should make a padding or an encoding to reduce the cipher size with big keys. Signed-off-by: HeshamTB <hishaminv@gmail.com>
30 lines
660 B
Python
30 lines
660 B
Python
#From: https://stackoverflow.com/questions/17298130/working-with-large-primes-in-python
|
|
from random import randrange
|
|
def is_prime(n, k=10):
|
|
if n == 2:
|
|
return True
|
|
if not n & 1:
|
|
return False
|
|
|
|
def check(a, s, d, n):
|
|
x = pow(a, d, n)
|
|
if x == 1:
|
|
return True
|
|
for i in range(s - 1):
|
|
if x == n - 1:
|
|
return True
|
|
x = pow(x, 2, n)
|
|
return x == n - 1
|
|
|
|
s = 0
|
|
d = n - 1
|
|
|
|
while d % 2 == 0:
|
|
d >>= 1
|
|
s += 1
|
|
|
|
for i in range(k):
|
|
a = randrange(2, n - 1)
|
|
if not check(a, s, d, n):
|
|
return False
|
|
return True |