# 分解模数n def rsa_moder(n):base = 2while base < n:if n % base == 0:return base, n // basebase += 1# 求欧拉函数f(n) def rsa_get_euler(prime1, prime2):return (prime1 - 1) * (prime2 - 1)# 求私钥 def rsa_get_key(e, euler):k = 1while True:if (((euler * k) + 1) % e) == 0:return (euler * k + 1) // ek += 1# 根据n,e计算d(或根据n,d计算e) def get_rsa_e_d(n, e=None, d=None):if e is None and d is None:returnarg = eif arg is None:arg = dprimes = rsa_moder(n)p = primes[0]q = primes[1]d = rsa_get_key(arg, rsa_get_euler(p, q))return ddef test():str_fmt = 'n: {:<10} e: {:<10} d: {:<10}'# 导入rsa库import rsa as rsakey = rsa.newkeys(24)# 产生rsa密钥对if isinstance(key[1], rsa.PrivateKey):print(str_fmt.format(key[1].n, key[1].e, key[1].d))# 解密n = 14666299d = 2101153e = get_rsa_e_d(n, None, d)print(str_fmt.format(n, e, d))n = 12748507e = 65537d = get_rsa_e_d(n, e, None)print(str_fmt.format(n, e, d))if __name__ == '__main__':test()