数字签名验证错误
Digital Signature Verification Errors
我正在对列表的元素进行加密,创建数字签名并验证该列表是否由我发送。但是,我在尝试验证时遇到错误。这是我的代码。请帮忙
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
###################### A Decryption Algorithm ##########################
def decrypt (counterPriv, encryptedVote, decrypt):
length = len(encryptedVote)
for index in range(length):
cipher = PKCS1_OAEP.new(counterPriv)
plaintext = cipher.decrypt(encryptedVote[index])
decrypt.append(plaintext)
######################################################################
############### My Personal Digital Signature Function ##############
def sign (privateKey, dataList, emptySig, hashList):
dataLength = len(dataList)
for index in range(dataLength):
h = SHA256.new()
h.update(dataList[index])
signature = pkcs1_15.new(privateKey).sign(h)
emptySig.append(signature)
hashList.append(h)
############################################################################
authen_key = RSA.generate(1024, e=65537)
authen_priv_key = authen_key.exportKey("PEM")
authen_public_key = authen_key.publickey().exportKey("PEM")
counter_key = RSA.generate(1024, e=65537)
counter_priv_key = counter_key.exportKey("PEM")
counter_public_key = counter_key.publickey().exportKey("PEM")
decrypt2 = []
myAnswer = ['Bob', 'Dylan', '12345']
myAnswerEncrypted = []
counterPublicKey = RSA.importKey(counter_public_key)
for index in range(3):
cipher = PKCS1_OAEP.new(counterPublicKey)
ciphertext = cipher.encrypt(myAnswer[index].encode("utf-8"))
myAnswerEncrypted.append(ciphertext)
decrypt(RSA.importKey(counter_priv_key), myAnswerEncrypted, decrypt2)
hashList = []
Signature_List = []
sign(RSA.importKey(authen_priv_key), decrypt2, Signature_List, hashList)
for index in range(len(hashList)):
try:
pkcs1_15.new(authen_key.publickey()).verify(hashList[index],
Signature_List[index])
except (ValueError, TypeError):
我在使用 .verify() 函数时遇到问题。计算机输出告诉我“AttributeError: 'bytes' object has no attribute 'n'”
有两个加密模块。 pycrypto 和 pycryptodome。我尝试过,您的代码可以与这两个模块一起使用。
使用 pycrypto 我需要将 pkcs1_15 重命名为 PKCS1_v1_5:
from Crypto.Signature import PKCS1_v1_5 as pkcs1_15
不过,我相信你与这两个模块有冲突,我会解释原因。
在 pycrypto 模块中,pkcs1_15.new(privateKey).sign(h)
returns 一个 byte
对象和 pycryptodome returns 一个 string
对象。这样,也许您已经使用了合并导入。在干净的环境中尝试运行。
我正在对列表的元素进行加密,创建数字签名并验证该列表是否由我发送。但是,我在尝试验证时遇到错误。这是我的代码。请帮忙
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
###################### A Decryption Algorithm ##########################
def decrypt (counterPriv, encryptedVote, decrypt):
length = len(encryptedVote)
for index in range(length):
cipher = PKCS1_OAEP.new(counterPriv)
plaintext = cipher.decrypt(encryptedVote[index])
decrypt.append(plaintext)
######################################################################
############### My Personal Digital Signature Function ##############
def sign (privateKey, dataList, emptySig, hashList):
dataLength = len(dataList)
for index in range(dataLength):
h = SHA256.new()
h.update(dataList[index])
signature = pkcs1_15.new(privateKey).sign(h)
emptySig.append(signature)
hashList.append(h)
############################################################################
authen_key = RSA.generate(1024, e=65537)
authen_priv_key = authen_key.exportKey("PEM")
authen_public_key = authen_key.publickey().exportKey("PEM")
counter_key = RSA.generate(1024, e=65537)
counter_priv_key = counter_key.exportKey("PEM")
counter_public_key = counter_key.publickey().exportKey("PEM")
decrypt2 = []
myAnswer = ['Bob', 'Dylan', '12345']
myAnswerEncrypted = []
counterPublicKey = RSA.importKey(counter_public_key)
for index in range(3):
cipher = PKCS1_OAEP.new(counterPublicKey)
ciphertext = cipher.encrypt(myAnswer[index].encode("utf-8"))
myAnswerEncrypted.append(ciphertext)
decrypt(RSA.importKey(counter_priv_key), myAnswerEncrypted, decrypt2)
hashList = []
Signature_List = []
sign(RSA.importKey(authen_priv_key), decrypt2, Signature_List, hashList)
for index in range(len(hashList)):
try:
pkcs1_15.new(authen_key.publickey()).verify(hashList[index],
Signature_List[index])
except (ValueError, TypeError):
我在使用 .verify() 函数时遇到问题。计算机输出告诉我“AttributeError: 'bytes' object has no attribute 'n'”
有两个加密模块。 pycrypto 和 pycryptodome。我尝试过,您的代码可以与这两个模块一起使用。
使用 pycrypto 我需要将 pkcs1_15 重命名为 PKCS1_v1_5:
from Crypto.Signature import PKCS1_v1_5 as pkcs1_15
不过,我相信你与这两个模块有冲突,我会解释原因。
在 pycrypto 模块中,pkcs1_15.new(privateKey).sign(h)
returns 一个 byte
对象和 pycryptodome returns 一个 string
对象。这样,也许您已经使用了合并导入。在干净的环境中尝试运行。