Python的RSA签名

Python的rsa签名方法

需要安装pycrypto

pip install pycrypto

python代码

#!-*- coding:utf-8 -*-

from Crypto.PublicKey import RSA
from Crypto.Hash import MD5
from Crypto.Signature import PKCS1_v1_5 as pk

def sign(signdata):
    privatekey = RSA.importKey(ASNKEY)
    h=MD5.new(signdata)
    signer = pk.new(privatekey)
    signn=signer.sign(h)
    signn=ByteToHex(signn)
    return signn

def ByteToHex(bins):
    return ''.join( [ "%02x" % x for x in bins ] ).strip()

def HexToByte(hexStr):
    return bytes.fromhex(hexStr)



ASNKEY = """-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALecq3BwAI4YJZwhJ+snnDFj3lF3DMqNPorV6y5ZKXCiCMqj8OeOmxk4YZW9aaV9
ckl/zlAOI0mpB3pDT+Xlj2sCAwEAAQJAW6/aVD05qbsZHMvZuS2Aa5FpNNj0BDlf38hOtkhDzz/h
kYb+EBYLLvldhgsD0OvRNy8yhz7EjaUqLCB0juIN4QIhAOeCQp+NXxfBmfdG/S+XbRUAdv8iHBl+
F6O2wr5fA2jzAiEAywlDfGIl6acnakPrmJE0IL8qvuO3FtsHBrpkUuOnXakCIQCqdr+XvADI/UTh
TuQepuErFayJMBSAsNe3NFsw0cUxAQIgGA5n7ZPfdBi3BdM4VeJWb87WrLlkVxPqeDSbcGrCyMkC
IFSs5JyXvFTreWt7IQjDssrKDRIPmALdNjvfETwlNJyY
-----END RSA PRIVATE KEY-----"""

PCKS8KEY = """-----BEGIN PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAt5yrcHAAjhglnCEn
6yecMWPeUXcMyo0+itXrLlkpcKIIyqPw546bGThhlb1ppX1ySX/OUA4jSakHekNP
5eWPawIDAQABAkBbr9pUPTmpuxkcy9m5LYBrkWk02PQEOV/fyE62SEPPP+GRhv4Q
Fgsu+V2GCwPQ69E3LzKHPsSNpSosIHSO4g3hAiEA54JCn41fF8GZ90b9L5dtFQB2
/yIcGX4Xo7bCvl8DaPMCIQDLCUN8YiXppydqQ+uYkTQgvyq+47cW2wcGumRS46dd
qQIhAKp2v5e8AMj9ROFO5B6m4SsVrIkwFICw17c0WzDRxTEBAiAYDmftk990GLcF
0zhV4lZvztasuWRXE+p4NJtwasLIyQIgVKzknJe8VOt5a3shCMOyysoNEg+YAt02
O98RPCU0nJg=
-----END PRIVATE KEY-----"""


header = "607875500OK15084842582741licensee=Administrator    licenseType=0   ";
content = "607875500OK15084842582741licensee=Administrator\tlicenseType=0\t"
print(sign(str.encode(content)))


update20171023:由于以上代码仅在非windows环境先容易实现,win安装pycrypto太费劲了

rsa签名可替换为纯python实现

安装rsa

pip install rsa

等价的签名代码示例

# -*- coding: UTF-8 -*-
import rsa


def ByteToHex(bins):
    return ''.join(["%02x" % x for x in bins]).strip()


def HexToByte(hexStr):
    return bytes.fromhex(hexStr)

ASNKEY = """-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALecq3BwAI4YJZwhJ+snnDFj3lF3DMqNPorV6y5ZKXCiCMqj8OeOmxk4YZW9aaV9
ckl/zlAOI0mpB3pDT+Xlj2sCAwEAAQJAW6/aVD05qbsZHMvZuS2Aa5FpNNj0BDlf38hOtkhDzz/h
kYb+EBYLLvldhgsD0OvRNy8yhz7EjaUqLCB0juIN4QIhAOeCQp+NXxfBmfdG/S+XbRUAdv8iHBl+
F6O2wr5fA2jzAiEAywlDfGIl6acnakPrmJE0IL8qvuO3FtsHBrpkUuOnXakCIQCqdr+XvADI/UTh
TuQepuErFayJMBSAsNe3NFsw0cUxAQIgGA5n7ZPfdBi3BdM4VeJWb87WrLlkVxPqeDSbcGrCyMkC
IFSs5JyXvFTreWt7IQjDssrKDRIPmALdNjvfETwlNJyY
-----END RSA PRIVATE KEY-----"""
privkey = rsa.PrivateKey.load_pkcs1(ASNKEY)
message = '607875500OK15084842582741licensee=Administrator\tlicenseType=0\t'
signature = rsa.sign(str.encode(message), privkey, 'MD5')
signature=ByteToHex(signature)
print(signature)

这样基本就能无痛跨平台了



你可能感兴趣的:(随想,python,md5,rsa,签名)