【发布时间】:2019-05-04 09:38:38
【问题描述】:
我正在编写一个程序来在 python 中使用 RSA 算法加密文件,而不使用 Crypto 库。我已经生成了密钥,并且 e、n 和 d 存储在一个 .pem 文件中。现在在另一个严格的加密发生的地方,我使用 e、d 和 n 值,但每次我运行脚本时都会显示错误:
File "rsaencrypt.py", line 91, in <module>
main()
File "rsaencrypt.py", line 62, in main
encrypt = pow(content, e, n)
TypeError: unsupported operand type(s) for pow(): 'bytes','_io.TextIOWrapper', '_io.TextIOWrapper'
这是我在加密脚本中打开文件并使用 pow() 加密文件的方式:
n = open('nfile.pem', 'r')
c = open('cfile.pem', 'r')
d = open('dfile.pem', 'r'))
encrypt = pow(content, e, n)
我已经在互联网上搜索了如何从文件中读取 int 值,但我什么也没找到。
以下是我在 efile、dfile 和 nfile 中保存值的方式:
#saving the values of n, d and e for further use
efile = open('efile.pem', 'w')
efile.write('%d' %(int(e)))
efile.close()
dfile = open('dfile.pem', 'w')
dfile.write('%d' %(int(d)))
dfile.close()
nfile = open('nfile.pem', 'w')
nfile.write('%d' % (int(n)))
nfile.close()
这些值的存储方式如下:564651648965132684135419864.......454
现在要加密文件,我需要读取写入 efile、dfile 和 nfile 中的整数值,以使用 pow() 中的值作为参数。
期待建议。谢谢。
【问题讨论】:
-
您是否尝试将读取的值转换为 int?就像您在写入文件时所做的一样
int(value) -
是的,我确实喜欢这个 n=int(open('filename', 'r')) ,但是另一个错误是显示 int() 参数必须是字符串、类似字节的对象或一个数字,而不是 '_io.TextIOWrapper'
标签: python python-3.x rsa file-handling public-key-encryption