def sign(message, prikey, encoding='utf8'): assert isinstance(message, str), 'message must be a sting!' assert isinstance(prikey, str), 'prikey must be a sting!' message = message.encode(encoding) prider = base64.b64decode(prikey) priv_key = rsa.PrivateKey.load_pkcs1(prider, 'DER') message_hash = hashlib.md5(message).digest() keylength = common.byte_size(priv_key.n) block_length = keylength - 11 assert block_length > 0, 'nbits of key is to small, please set bigger then 128!' signature = b'' while message_hash: cleartext = message_hash[:block_length] message_hash = message_hash[block_length:] # ===== copy from rsa.pkcs1:sign_hash ===== padded = _pad_for_signing(cleartext, keylength) payload = transform.bytes2int(padded) encrypted = priv_key.blinded_encrypt(payload) block = transform.int2bytes(encrypted, keylength) signature += block signature = base64.b64encode(signature).decode() return signature
def encrypt(self, message, file): self._load_key_file(file) keylength = common.byte_size(self._private_key.n) padded = pkcs1._pad_for_signing(bytes(message, encoding="utf-8"), keylength) payload = transform.bytes2int(padded) encrypted = self._private_key.blinded_encrypt(payload) block = transform.int2bytes(encrypted, keylength) return base64.urlsafe_b64encode(block).decode("utf-8")
def signature_hhj(after_hash, private_key, encoding='utf8'): after_hash1 = str(after_hash) after_hash2 = after_hash1.encode(encoding) prider = base64.b64decode(private_key) priv_key = rsa.PrivateKey.load_pkcs1(prider, 'DER') keylength = common.byte_size(priv_key.n) block_length = keylength - 11 assert block_length > 0, 'nbits of key is to small, please set bigger then 128!' signature = b'' while after_hash2: cleartext = after_hash2[:block_length] after_hash2 = after_hash2[block_length:] # ===== copy from rsa.pkcs1:sign_hash ===== padded = _pad_for_signing(cleartext, keylength) payload = transform.bytes2int(padded) encrypted = priv_key.blinded_encrypt(payload) block = transform.int2bytes(encrypted, keylength) signature += block signature = base64.b64encode(signature).decode() #print("--------------签名成功--------------") return signature