全晚,
我想使用Public Key加密验证文件,但我无法弄清楚为什么我会为下面的代码收到类型错误,请注意signatureLength = 512
signature = f[:signatureLength]
f = open('lib/publicKey.pem','rb')
publicKey = RSA.importKey(f.read())
hash = SHA512.new(f[signatureLength:])
verification = PKCS1_PSS.new(publicKey)
具体错误是:
File "C:\Users\Zach Newton\Desktop\pp\lib\files.py", line 77, in verify_file
hash = SHA512.new(f[signatureLength:])
TypeError: '_io.BufferedReader' object is not subscriptable
答案 0 :(得分:1)
您正在重新分配名称f
:
signature = f[:signatureLength]
f = open('lib/publicKey.pem','rb')
publicKey = RSA.importKey(f.read())
hash = SHA512.new(f[signatureLength:]) # <-- this isn't the same f anymore
verification = PKCS1_PSS.new(publicKey)
你应该使用这样的东西:
signature = f[:signatureLength]
with open('lib/publicKey.pem','rb') as pubkeyfile:
publicKey = RSA.importKey(pubkeyfile.read())
hash = SHA512.new(signature)
verification = PKCS1_PSS.new(publicKey)
出于这个原因,使用通用变量名称如f
并且不鼓励使用名称来完全不同。