有些版次的車輪沒造好,就自己造,以下為教科書版能處理1024-bit RSA的python程式
print('利用sympy、numpy來實作RSA演算,含字串的加解密')
import sympy
import numpy as np
?sympy
先產生質數
def generate_prime():
prime = sympy.randprime(2**511, 2**512)
return prime
p=generate_prime()
q=generate_prime()
print('p=',p,'\nq=',q)
print(type(p))
n=p*q
print('模數n=',n)
公鑰e與私鑰d
phi_n=(p-1)*(q-1)
print('phi_n=',phi_n)
lambda_n=phi_n/sympy.gcd(p-1, q-1)
lambda_n
e=sympy.randprime(phi_n//2, phi_n)
print('公鑰e=',e)
?sympy.gcdex
d, _, gcd = sympy.gcdex(e, phi_n)
if gcd!=1:
print('gcd(e, phi_n)!=1錯')
d=(d+phi_n)%phi_n
print("私鑰d=",d)
e*d%phi_n==1
沒有留言:
張貼留言