使用DSA(数字签名算法)在文本文件中签名和验证数据

时间:2018-03-03 12:18:28

标签: python-2.7 cryptography key rsa digital-signature

我想使用Python中的数字签名算法在文本文档(.txt)中签名数据。我使用RSA算法生成了公钥和私钥。然后我使用下面给出的代码签名并验证数据。

from __future__ import print_function
import cv2
import numpy as np
import rsa
from base64 import b64encode, b64decode
import base64
f=open("2.txt",'r') 
msg1=f.read()
keysize = 2048
(public,private) = rsa.newkeys(keysize)

#encrypted = b64encode(rsa.encrypt(msg1, public))

signature = b64encode(rsa.sign(msg1, private, "SHA-512"))

print("Signature: " + signature)

用于在接收方验证

from __future__ import print_function
import os
from PIL import Image
import cv2
import numpy as np
import rsa
import base64
from base64 import b64encode, b64decode
from digsig import public
from digsig import signature
f1=open("2.txt",'r') 
msg1=f1.read()
f=open("3.txt",'r') 
msg2=f.read()


#decrypted = rsa.decrypt(b64decode(msg2), private)
#print("Decrypted: '%s'" % decrypted)

verify = rsa.verify(msg1, b64decode(signature), public)
print("Verify: %s" % verify)
rsa.verify(msg2, b64decode(signature), public)

此Rsa.py已定义函数

rsa.py

    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
    from Crypto import Random

from base64 import b64encode, b64decode

hash = "SHA-256"

def newkeys(keysize):
    random_generator = Random.new().read
    key = RSA.generate(keysize, random_generator)
    private, public = key, key.publickey()
    return public, private

def importKey(externKey):
    return RSA.importKey(externKey)

def getpublickey(priv_key):
    return priv_key.publickey()

def encrypt(message, pub_key):
    #RSA encryption protocol according to PKCS#1 OAEP
    cipher = PKCS1_OAEP.new(pub_key)
    return cipher.encrypt(message)

def decrypt(ciphertext, priv_key):
    #RSA encryption protocol according to PKCS#1 OAEP
    cipher = PKCS1_OAEP.new(priv_key)
    return cipher.decrypt(ciphertext)



def sign(message, priv_key, hashAlg="SHA-256"):
    global hash
    hash = hashAlg
    signer = PKCS1_v1_5.new(priv_key)
    if (hash == "SHA-512"):
        digest = SHA512.new()
    elif (hash == "SHA-384"):
        digest = SHA384.new()
    elif (hash == "SHA-256"):
        digest = SHA256.new()
    elif (hash == "SHA-1"):
        digest = SHA.new()
    else:
        digest = MD5.new()
    digest.update(message)
    return signer.sign(digest)

def verify(message, signature, pub_key):
    signer = PKCS1_v1_5.new(pub_key)
    if (hash == "SHA-512"):
        digest = SHA512.new()
    elif (hash == "SHA-384"):
        digest = SHA384.new()
    elif (hash == "SHA-256"):
        digest = SHA256.new()
    elif (hash == "SHA-1"):
        digest = SHA.new()
    else:
        digest = MD5.new()
    digest.update(message)
    return signer.verify(digest, signature)

因此,在验证数据时,如果我更改文件中的某些数据。我得到验证程序为真,即使它们不相同,两个数据都是相同的。我无法弄清楚为什么会这样。我没有使用DSA算法,只是遵循数字签名的程序,即私钥用于加密发送方的数据,并使用接收方的公钥解密该数据,然后验证文件中的两个数据。

1 个答案:

答案 0 :(得分:0)

您只能生成私钥/公钥对。试试RSA.generate(bits)