天天看点

python关于sha1rsa加密简化版

import rsa
import hashlib
import binascii

msg = "write here what you want"
def sign(data, key_file):
    """sign

    @param data: data to be signed
    @param key_file: private key file path
    @return: a hex string which is the signature
    """
    key_file = open(key_file, 'rb').read()
    signature = rsa.sign(data, rsa.PrivateKey.load_pkcs1(key_file), 'SHA-1')
    return binascii.hexlify(signature)

a = sign(msg,"private.pem")
print(a)      

#这个方法得到的sign值(也就是转16进制后)是256长度的,上篇博客加密后转16进制值512长度的,本篇方法使用的是加签加密一起执行的,不像上篇博客那样严谨,是先加签再分段加密从而进行解签解密,各有优劣,主要看业务要求。