PyCrypto:属性错误:'NoneType'对象在使用PKCS1_v1_5时没有属性'oid'

时间:2015-10-25 12:43:38

标签: python hash rsa digital-signature pycrypto

SHA = hashlib.sha1()
Eh = SHA.update(chunk)
HRSA.signSHA(Eh,RSAprivatekey)

在HRSA模块中读取RSAprivatekey并将其作为参数传递给此函数:

RSAprivatekey = RSA.importKey(infile.read())

infile指向仅包含RSAprivatekey的'privatekey.txt'。

HRSA是我创建的模块,基本上是这样做的:

def signSHA(hash, key):
    signer = PKCS1_v1_5.new(key)
    D = signer.sign(hash)
    return D

我出现以下错误:

File "D:\Study\Sem V\Hybrid Encryption\Phase 2\HRSA.py", line 57, in signSHA
    D = signer.sign(hash)
  File "C:\Python33\lib\site-packages\Crypto\Signature\PKCS1_v1_5.py", line 110, in sign
    em = EMSA_PKCS1_V1_5_ENCODE(mhash, k)
  File "C:\Python33\lib\site-packages\Crypto\Signature\PKCS1_v1_5.py", line 211, in EMSA_PKCS1_V1_5_ENCODE
    digestAlgo  = DerSequence([hash.oid, DerNull().encode()])
AttributeError: 'NoneType' object has no attribute 'oid'

我怎么能解决这个问题,因为这是PyCrypto代码的错误?

2 个答案:

答案 0 :(得分:1)

hashfunc.update(arg)不会返回任何内容。它用新的输入数据更新散列函数的内部状态。如果要将内部状态转换为哈希值,则需要调用hashfunc.digest()hashfunc.hexdigest()

看起来像这样:

Eh = hashlib.sha1(chunk).digest()
HRSA.signSHA(Eh, RSAprivatekey)

答案 1 :(得分:1)

您必须将PyCrypto模块用于SHA1,因此:

from Crypto.Hash import SHA1
sha_obj = SHA1.new()
sha_obj.update(chunk)
HRSA.signSHA(sha_obj,RSAprivatekey)

原因是PKCS#1v1.5签名嵌入了散列的ASN.1对象ID,但从标准库获取的SHA-1对象不包含/知道它。