2014-01-14 75 views
0

嗨我想創建一個有效的RSA程序,但在一個非常小的級別上,我遇到了使用此代碼進行加密和解密的問題,有人可以幫我弄清楚什麼是錯誤的?我嘗試過很多不同的方式,但這種方式似乎是正確的數學方法,所以我相信這可能只是我缺乏編碼技能?由於簡單的RSA代碼

import random, math 

def RandomPrime(): 
    prime = False 
    while prime == False: 
    n = 2 
    while n % 2 == 0: 
     n = random.randint(10000, 100000) 
    s = math.trunc(n**0.5) 
    s = int(s) 
    x = 3 
    # While n doesn't exactly divide to equal 0, and x is less then the sqrt of n 
    while (n % x != 0) and (x <= s): 
     x = x + 2 
    # if n is greater than s, it means it has run out of numbers to test, so is prime 
    if x > s: 
     prime = True 




    return n 

def Modulus(p, q): 
    M = p * q 
    return M 

def Totient(p, q): 
    T = ((p-1) * (q-1)) 
    return T 

def Pubkey(T): 
    prime = False 
    while prime == False: 
    n = 2 
    while n % 2 == 0: 
     n = random.randint(3, T) 
    s = math.trunc(n**0.5) 
    s = int(s) 
    x = 3 
    # While 
    while (n % x != 0) and (x <= s): 
     x = x + 2 
    if x > s: 
     prime = True 
    return n 

def privkey(T, n): 
    y = math.fmod(1, T) 
    d = float((y/n)) 
    return d 


# z is my encyption in this scenario 
z = 8 
# I generate p and q, using my random prime generator, i used low primes in 
# this example just to see if it would work but it is still not showing reults 
p = RandomPrime() 
q = RandomPrime() 
print(p, q) 
#This creates the modulus 
M = Modulus(p, q) 
print(M) 
# Eulier's totient 
T = Totient(p, q) 
print(T) 
#Pub key creation 
n = Pubkey(T) 
print(n) 
#Priv key creation 
d = privkey(n, T) 
print(d) 


enc = (pow(z, n)) % M 
print('enc: ', enc) 


dec = (pow(enc, d)) % M 
print('dec: ', dec) 
+0

你錯過了告訴我們您所使用的編程語言。編輯您的問題並至少將其添加爲標籤。 – Robert

+0

其蟒蛇,對不起夥伴 – user3181295

+0

你的錯誤是什麼?你能計算(pow(z,n))嗎?它應該是一個任意n的巨大數字。 – alko

回答

0

privkey功能似乎錯了 - 我猜你看到RSA私鑰值的定義是這樣的:

the value "e" such that e * d = 1 mod Phi(N)

然而,在這種情況下,1 mod Phi(N)意味着The remainder when 1 is divided by Phi(N) (這似乎是你將它翻譯成代碼的方式,根據你使用的math.fmod(1, T),但實際上應該看起來更像:

the value "e" such that (e * d) mod Phi(N) = 1

該值通常使用Extended Euclidean Algorithm來計算。 Python實現的一個示例是here

同樣值得注意的是,您似乎將privkey(T, n)定義爲privkey(n, T)

+0

我應該如何重新排列該公式才能得到d? – user3181295

+0

@ user3181295您將需要使用算法,如擴展歐幾里德算法(在上面的答案中鏈接)來執行計算。 – Iridium

+0

我在python中這樣做很困難,你能幫助嗎? – user3181295

0

檢查我的博客裏面詳細包含以下使用python的實現:

MD5安全散列算法RFC 1321,RSA公鑰加密RFC 3447,OpenPGP的RFC 4880

def keyGen(): 
    ''' Generate Keypair ''' 
    i_p=randint(0,20) 
    i_q=randint(0,20) 
    # Instead of Asking the user for the prime Number which in case is not feasible, 
    # generate two numbers which is much highly secure as it chooses higher primes 
    while i_p==i_q: 
     continue 
    primes=PrimeGen(100) 
    p=primes[i_p] 
    q=primes[i_q] 
    #computing n=p*q as a part of the RSA Algorithm 
    n=p*q 
    #Computing lamda(n), the Carmichael's totient Function. 
    # In this case, the totient function is the LCM(lamda(p),lamda(q))=lamda(p-1,q-1) 
    # On the Contrary We can also apply the Euler's totient's Function phi(n) 
    # which sometimes may result larger than expected 
    lamda_n=int(lcm(p-1,q-1)) 
    e=randint(1,lamda_n) 
    #checking the Following : whether e and lamda(n) are co-prime 
    while math.gcd(e,lamda_n)!=1: 
     e=randint(1,lamda_n) 
    #Determine the modular Multiplicative Inverse 
    d=modinv(e,lamda_n) 
    #return the Key Pairs 
    # Public Key pair : (e,n), private key pair:(d,n) 
    return ((e,n),(d,n)) 

博客鏈接: Python Cryptography

Github上鍊接:Python Cryptography

+0

博客鏈接:[Python密碼學](http://crypto-python.blogspot.com) –