嗨我想创建一个有效的RSA程序,但在一个非常小的级别上,我遇到了使用此代码进行加密和解密的问题,有人可以帮我弄清楚什么是错误的?我尝试过很多不同的方式,但这种方式似乎是正确的数学方法,所以我相信这可能只是我缺乏编码技能?由于简单的RSA代码
import random, math
def RandomPrime():
prime = False
while prime == False:
n = 2
while n % 2 == 0:
n = random.randint(10000, 100000)
s = math.trunc(n**0.5)
s = int(s)
x = 3
# While n doesn't exactly divide to equal 0, and x is less then the sqrt of n
while (n % x != 0) and (x <= s):
x = x + 2
# if n is greater than s, it means it has run out of numbers to test, so is prime
if x > s:
prime = True
return n
def Modulus(p, q):
M = p * q
return M
def Totient(p, q):
T = ((p-1) * (q-1))
return T
def Pubkey(T):
prime = False
while prime == False:
n = 2
while n % 2 == 0:
n = random.randint(3, T)
s = math.trunc(n**0.5)
s = int(s)
x = 3
# While
while (n % x != 0) and (x <= s):
x = x + 2
if x > s:
prime = True
return n
def privkey(T, n):
y = math.fmod(1, T)
d = float((y/n))
return d
# z is my encyption in this scenario
z = 8
# I generate p and q, using my random prime generator, i used low primes in
# this example just to see if it would work but it is still not showing reults
p = RandomPrime()
q = RandomPrime()
print(p, q)
#This creates the modulus
M = Modulus(p, q)
print(M)
# Eulier's totient
T = Totient(p, q)
print(T)
#Pub key creation
n = Pubkey(T)
print(n)
#Priv key creation
d = privkey(n, T)
print(d)
enc = (pow(z, n)) % M
print('enc: ', enc)
dec = (pow(enc, d)) % M
print('dec: ', dec)
你错过了告诉我们您所使用的编程语言。编辑您的问题并至少将其添加为标签。 – Robert
其蟒蛇,对不起伙伴 – user3181295
你的错误是什么?你能计算(pow(z,n))吗?它应该是一个任意n的巨大数字。 – alko