在Python中实现RSA

发布于 2024-09-11 00:21:43 字数 1945 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枉心 2024-09-18 00:21:43

您的问题很可能出在浮点运算的使用上:

        result = (count * phyQP + 1) / float(pubKeyExpo)

在此算法中,自始至终使用任意精度整数运算非常重要。

pow() 的三参数版本在您的实施中的一些地方将会很有用。 pow(x, y, z) 计算任意精度整数的 (x ** y) mod z

Your problem is most likely in your use of floating point arithmetic:

        result = (count * phyQP + 1) / float(pubKeyExpo)

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.

绝影如岚 2024-09-18 00:21:43

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/

毅然前行 2024-09-18 00:21:43

您不能使用普通的数值计算进行加密。
数字的指数通常为 1000。
使用Python库,例如gmpy2,可以处理大整数的计算

import gmpy2
然后,例如,更改:

结果 = (计数 * phyQP + 1) / float(pubKeyExpo)

为:

结果 = gmpy2.f_divmod(count*phyQP + 1, pubKeyExpo)

如果结果[0]>0且结果[1]==0:

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:

result = (count * phyQP + 1) / float(pubKeyExpo)

to:

result = gmpy2.f_divmod(count*phyQP + 1, pubKeyExpo)

if result[0]>0 and result[1]==0:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文