在Python中实现RSA
我正在尝试在 python 中实现 RSA(我是 python 新手),我的问题是我编写的代码不适用于超过 4 位数字的数字。知道为什么会发生这种情况吗?请指教
p =0
q=0
n=0#modules
phyPQ = 0
e = 0 #public key exponent
d = 0#private key exponent
c = ''
m = ''
def getPrimes():
global p
global q
p = long(raw_input("Enter first prime p : "))
q = long(raw_input("Enter second prime q : "))
def computeModules(prime1, prime2):
global n
n = prime1 * prime2
print "Modules is = "+ `n`
return n
def computePhyPQ(prime1, prime2):
global phyPQ
phyPQ = (prime1 -1) * (prime2 -1)
print "The phyPQ is " + `phyPQ`
def computePublickeyExponent(x):
pubKeyExponentList= []
for i in range(1, x):
if x % i != 0:
pubKeyExponentList.append(i)
print pubKeyExponentList
global e
e = long(raw_input("Pick a public key exponent from the list above : "))
def computePrivateKeyExponent(phyQP, pubKeyExpo):
flag = 1
count = 0
while flag == 1:
count = count + 1
if (count * phyQP + 1) % phyQP == 1:
result = (count * phyQP + 1) / float(pubKeyExpo)
if result % 1 == 0:
global d
d = long(result)
print 'The private key exponent exponent is:' + `d`
flag = 0
def encryptMessage(exponent, modules):
#c= m ^e mod n
global c
message= long(raw_input("Enter a value to be encrypted:"))
c = long((message ** exponent) % modules)
print'The encrypted message is :' + `c`
def decryptMessage(modules, privateKeyExpo, cryptedMessage):
#m = c^d % n
global m
m = (cryptedMessage ** privateKeyExpo) % modules
print 'message after decrypting is :' + `m`
def mainMethod():
getPrimes()
computeModules(p, q)
computePhyPQ(p, q)
computePublickeyExponent(phyPQ)
computePrivateKeyExponent(phyPQ, e)
encryptMessage(e, n)
decryptMessage(n, d, c)
mainMethod()
i am trying to implement RSA in python(i am new to python) for my course, the problem i have is the code i have written does not work for numbers with more than 4 digits. any idea why this is happening? please advice
p =0
q=0
n=0#modules
phyPQ = 0
e = 0 #public key exponent
d = 0#private key exponent
c = ''
m = ''
def getPrimes():
global p
global q
p = long(raw_input("Enter first prime p : "))
q = long(raw_input("Enter second prime q : "))
def computeModules(prime1, prime2):
global n
n = prime1 * prime2
print "Modules is = "+ `n`
return n
def computePhyPQ(prime1, prime2):
global phyPQ
phyPQ = (prime1 -1) * (prime2 -1)
print "The phyPQ is " + `phyPQ`
def computePublickeyExponent(x):
pubKeyExponentList= []
for i in range(1, x):
if x % i != 0:
pubKeyExponentList.append(i)
print pubKeyExponentList
global e
e = long(raw_input("Pick a public key exponent from the list above : "))
def computePrivateKeyExponent(phyQP, pubKeyExpo):
flag = 1
count = 0
while flag == 1:
count = count + 1
if (count * phyQP + 1) % phyQP == 1:
result = (count * phyQP + 1) / float(pubKeyExpo)
if result % 1 == 0:
global d
d = long(result)
print 'The private key exponent exponent is:' + `d`
flag = 0
def encryptMessage(exponent, modules):
#c= m ^e mod n
global c
message= long(raw_input("Enter a value to be encrypted:"))
c = long((message ** exponent) % modules)
print'The encrypted message is :' + `c`
def decryptMessage(modules, privateKeyExpo, cryptedMessage):
#m = c^d % n
global m
m = (cryptedMessage ** privateKeyExpo) % modules
print 'message after decrypting is :' + `m`
def mainMethod():
getPrimes()
computeModules(p, q)
computePhyPQ(p, q)
computePublickeyExponent(phyPQ)
computePrivateKeyExponent(phyPQ, e)
encryptMessage(e, n)
decryptMessage(n, d, c)
mainMethod()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题很可能出在浮点运算的使用上:
在此算法中,自始至终使用任意精度整数运算非常重要。
pow()
的三参数版本在您的实施中的一些地方将会很有用。pow(x, y, z)
计算任意精度整数的(x ** y) mod z
。Your problem is most likely in your use of floating point arithmetic:
In this algorithm, it will be important to use arbitrary precision integer arithmetic throughout.
The three-argument version of
pow()
will be useful in a few places in your implementation.pow(x, y, z)
calculates(x ** y) mod z
for arbitrary-precision integers.c = long((message ** exponent) %modules)
不是一个正确的实现,因为它非常慢。您可以将其替换为平方乘幂运算、滑动窗口幂运算或蒙哥马利梯形运算。
一个很好的例子可以在这里找到: http://code.activestate.com/recipes/572196- RSA/
c = long((message ** exponent) % modules)
is not a proper implementation, since it is prohibitively slow.You could replace it with square-and-multiply exponentiation, sliding-window exponentiation, or Montgomery powering ladder.
A good example can be found here: http://code.activestate.com/recipes/572196-rsa/
您不能使用普通的数值计算进行加密。
数字的指数通常为 1000。
使用Python库,例如gmpy2,可以处理大整数的计算
import gmpy2
然后,例如,更改:
为:
You cannot use normal numeric calculations for encryption.
Numbers typically has an exponent of 1000.
Use a python library such as gmpy2 that can handle calculations with huge integers
import gmpy2
Then, for example, change:
to: