我正在关注 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()