0

我正在关注 Wiki 中的 RSA 算法:http://en.wikipedia.org/wiki/RSA_(algorithm)

我正在使用 Python 3.3.0,并且正在尝试进行 RSA 加密,但遇到了两个我不知道该怎么做的问题。

在 Encryptions 类中,我的方法都需要缩进一级以表明它们是类的方法而不是全局函数。

当主脚本最后要求输入时,如果我只是按回车键,则会引发 Python 到达意外 EOF 的异常。

我怎样才能做到这一点 ?

到目前为止我的代码:

模块化.py

def _base_b_convert(n, b):
   if b < 1 or n < 0:
      raise ValueError("Invalid Argument")

   q = n
   a = []
   while q != 0:
      value = int(q % b)
      a.append(value)
      q =int(q / b)
   return a


def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
    raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
    if a[i] == 1:
        x = (x * pow) % mod
    pow = pow**2 % mod
    return x

主文件

from encryptions import Encryptions

def main():
    enc = Encryptions()
    message = enc.encrypt(message)
    print(message)
    print()
    print("Decrypting message:")
    message = enc.decrypt(message)
    print(message)

    input("--Press any key to end--")

if __name__ == '__main__':
    main()
4

2 回答 2

2

您的缩进已关闭。有时你有 3 个空格,有时 4 个,有时 5 个。

另一个例子是here

def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
    raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
    if a[i] == 1:
        x = (x * pow) % mod
    pow = pow**2 % mod
    return x

它应该看起来更像

def mod_exp(base, n, mod):
    if base < 0 or n < 0 or mod < 0:
        raise ValueError("Invalid Argument")
    a = (_base_b_convert(n, 2))
    x = 1
    pow = base % mod
    for i in range(0, len(a)):
        if a[i] == 1:
            x = (x * pow) % mod
        pow = pow**2 % mod
    return x

每当您使用 if、while、for 等时,您都需要缩进一层。

(这些问题可能只是因为它被错误地复制到了stackoverflow中?)

于 2013-02-17T15:12:34.097 回答
2

input()在 Python 2 中不是你想的那样——相反,它评估作为 Python 代码输入的字符串,这不是你想要的。相反,使用raw_input.

至于你的缩进问题,那只是 Python 语法。你无能为力。

于 2013-02-17T15:07:45.937 回答