简单的 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)

您的privkey函数似乎错误 - 我猜您看到 RSA 私钥值的定义如下:

值 "e" 使得 e * d = 1 mod Phi(N)

但是在这种情况下,1 mod Phi(N)并不意味着The remainder when 1 is divided by Phi(N)(这似乎是您根据math.fmod(1, T)的使用将其转换为代码的方式,但实际上应该更像:

值 "e" 使得 (e * d) mod Phi(N) = 1

此值通常使用扩展欧几里得算法计算。这里有一个示例 Python 实现。

还值得注意的是,您似乎在定义privkey(T, n)但将其称为privkey(n, T)

查看我的博客,其中详细包含使用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))

博客链接 :P ython 密码学

Github链接:Python Cryptography

最新更新