这段代码会给出RSA密钥的正确值(假设其他函数是正确的)吗?我很难让我的程序正确解密,因为在某些块中没有正确地解密
这是python中的:
import random
def keygen(bits):
p = q = 3
while p == q:
p = random.randint(2**(bits/2-2),2**(bits/2))
q = random.randint(2**(bits/2-2),2**(bits/2))
p += not(p&1) # changes the values from
q += not(q&1) # even to odd
while MillerRabin(p) == False: # checks for primality
p -= 2
while MillerRabin(q) == False:
q -= 2
n = p * q
tot = (p-1) * (q-1)
e = tot
while gcd(tot,e) != 1:
e = random.randint(3,tot-1)
d = getd(tot,e) # gets the multiplicative inverse
while d<0: # i can probably replace this with mod
d = d + tot
return e,d,n生成了一组密钥:
E=3 daf16a37799d3b2c951c9baab30ad2d
D= 16873c0dd2825b2e8e6c2c68da3a5e25
N= dc2a732d64b83816a99448a2c2077ced
发布于 2010-05-10 08:25:03
我想你这样做是为了好玩和学习,而不是为了某种需要实际安全的东西。
下面是我注意到的一些事情(没有特别的顺序):
bits。它可能和bits - 4一样短。random不是加密安全的随机数生成器。e为65537是常见的(也同样安全)。这是一个质数,所以你可以用除数检查来代替互换性检查。e来搜索e = tot有点奇怪(铜检查肯定会失败)。否则就没问题了。关键似乎也很好。您有一个没有正确解密的块的示例吗?请记住,您只能加密小于n的数据。因此,使用128位密钥(在您的示例中),您不能加密所有128位数字。
https://stackoverflow.com/questions/2799503
复制相似问题