【问题标题】:I have some error with my code, can someone please review it once [closed]我的代码有一些错误,有人可以检查一下[关闭]
【发布时间】:2021-11-18 02:11:12
【问题描述】:

[https://github.com/Simrantiwarii/RSA-ENCYRPTION/blob/1e88b32c9d1e133886f73115374d5018d946f5b8/Untitled6.ipynb] 有人可以查看此代码并告诉我为什么会出现此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-43-01ba3f5a9a3e> in <module>
     88 
     89 if __name__ == '__main__':
---> 90     public_key,private_key = generate_keyPairs()
     91     print("Public: ",public_key)
     92     print("Private: ",private_key)

<ipython-input-43-01ba3f5a9a3e> in generate_keyPairs()
     53         E = generatenextPrime(E)
     54         #print(type(F))
---> 55         g = gcd(E,phi)
     56     #print("E=",E,)
     57     #print(type(E))

<ipython-input-43-01ba3f5a9a3e> in gcd(a, b)
     11 def gcd(a, b):
     12     while b != 0:
---> 13         a, b = b, a % b
     14     return a
     15 

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

我正在尝试研究 RSA 中加密密钥组件“e”的性质。

【问题讨论】:

    标签: python encryption jupyter-notebook rsa public-key-encryption


    【解决方案1】:

    正如错误消息所说:a 不是数字而是无。我假设generatenextPrime(E) 返回无

    def generatenextPrime(num):
        E = num+1
        #print ("E= %d" %E)
        if is_prime(E):
            return E
        else:
            num += 1  # <--- nothing (None) is returned in that case
    

    试试这个

    def generatenextPrime(num):
        e = num
        while not is_prime(e):
            e += 1
        return e
    
            
    

    【讨论】:

    • 能否请您查看我的代码,并告诉我应该在 generatenextPrime(E) 函数中进行哪些更改,以便将下一个素数提供给 E?
    • generatenextPrime 未包含在您的问题中
    • ^ 我更新了答案:)
    • 你能帮帮我吗,告诉我我到底需要改变什么,以便generatenextPrime返回到相同的递增循环并返回一个质数而不是NONE。
    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 2021-12-06
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2014-01-13
    • 2015-01-28
    相关资源
    最近更新 更多