Skip to main content
 首页 » 编程设计

python之python RSA中相同 key 的不同解密输出

2025年05月04日7cmt

我正在编写一个 RSA 的小实现以帮助我在大学学习类(class),但我遇到了一个我无法修复的错误。

这是我目前的代码:

import numpy 
 
def primesfrom2to(n): 
    """ Input n>=6, Returns a array of primes, 2 <= p < n  
        Taken from: http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 """ 
    sieve = numpy.ones(n/3 + (n%6==2), dtype=numpy.bool) 
    for i in xrange(1,int(n**0.5)/3+1): 
        if sieve[i]: 
            k=3*i+1|1 
            sieve[       k*k/3     ::2*k] = False 
            sieve[k*(k-2*(i&1)+4)/3::2*k] = False 
    return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)] 
 
def extended_gcd(a, b): 
    if b == 0: 
        return (1, 0) 
    else: 
        q = a / b 
        r = a - b * q 
        s, t = extended_gcd(b, r) 
        return (t, s - q * t) 
 
def pick_e(phi): 
    primes = primesfrom2to(phi) 
    e = primes[0] 
    i = 0 
    while phi % e != 1 and 1 < e < phi: 
        i += 1 
        e = primes[i] 
    return e 
 
def RSA(p, q, e=None): 
    n = p * q 
    phi = (p-1) * (q-1) 
    if not e: 
        e = pick_e(phi) 
    x, y = extended_gcd(e, phi) 
    d = x % phi 
    return (e, n), (d, n) 
 
def encrypt(P, public_key): 
    C = P**public_key[0] % public_key[1] 
    return C 
 
def decrypt(C, private_key): 
    P = C**private_key[0] % private_key[1] 
    return P 
 
public_key, private_key = RSA(11, 13) 
public_key2, private_key2 = RSA(11, 13, 7) 
print public_key, private_key 
print public_key2, private_key2 
 
print "public_key and private_key" 
print "plaintext -> ciphertext -> plaintext" 
for i in range(1,10): 
    c =  encrypt(i, public_key) 
    p = decrypt(c, private_key) 
    print "%9d -> %10d -> %9d" % (i, c, p) 
 
print "public_key2 and private_key2" 
print "plaintext -> ciphertext -> plaintext" 
for i in range(1,10): 
    c =  encrypt(i, public_key2) 
    p = decrypt(c, private_key2) 
    print "%9d -> %10d -> %9d" % (i, c, p) 

我得到的输出是:

(7, 143) (103, 143) 
(7, 143) (103, 143) 
public_key and private_key 
plaintext -> ciphertext -> plaintext 
        1 ->          1 ->         1 
        2 ->        128 ->         0 
        3 ->         42 ->         0 
        4 ->         82 ->         0 
        5 ->         47 ->        15 
        6 ->         85 ->       126 
        7 ->          6 ->         0 
        8 ->         57 ->        47 
        9 ->         48 ->         0 
public_key2 and private_key2 
plaintext -> ciphertext -> plaintext 
        1 ->          1 ->         1 
        2 ->        128 ->         2 
        3 ->         42 ->         3 
        4 ->         82 ->         4 
        5 ->         47 ->         5 
        6 ->         85 ->         6 
        7 ->          6 ->         7 
        8 ->         57 ->         8 
        9 ->         48 ->         9 

两者的输出应该相同,因为键是相同的。谁能看出我做错了什么?

请您参考如下方法:

您的 pick_e 函数显然返回的是 numpy.int64 而不是 int;如果您将 return 语句替换为

return int(e) 

您的代码按预期工作(至少对我来说 :-) )