2011-04-28 73 views
2

我正在編寫RSA的一個小實施,以幫助我在大學學習一門課,我遇到了一個我無法修復的錯誤。不同的解密輸出爲相同的密鑰在Python RSA

這裏是我到目前爲止的代碼:

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 

輸出兩個應該是相同的,因爲密鑰是相同的。任何人都可以看到我做錯了什麼?

+0

這不會回答你的問題,但你可以驗證你的代碼做正確的事與[pycrypto](http://www.dlitz.net/software/pycrypto/)。 – nmichaels 2011-04-28 19:14:18

+0

它是與這個難題: (PDB)'C == 128' 真 (PDB)'(128 ** 103%143)==(C ** 103%143)' 假 – 2011-04-28 19:23:50

回答

1

你的pick_e函數顯然返回一個numpy.int64而不是int;如果你有

return int(e) 

你的代碼工作正常更換return語句(至少對我來說:-))

+0

這,除了它是一個numpy.int64而不是一個浮點數。這就是我上面的評論原來的原因。 – 2011-04-28 19:33:15

+0

此外,如果您使用math.pow而不是**,解密將在沒有@Frank Schmitt建議的int cast的情況下工作。我對此沒有解釋。 – 2011-04-28 19:35:16

+0

感謝您的提示,我已經糾正它(用numpy.int64替換float) – 2011-04-28 19:46:46