【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
def openssl_private_encrypt(key, data, backend):
"""Encrypt data with RSA private key.
This is a rewrite of the function from PHP, using cryptography
FFI bindings to the OpenSSL library. Private key encryption is
non-standard operation and Python packages either don't offer
it at all, or it's incompatible with the PHP version.
The backend argument MUST be the OpenSSL cryptography backend.
"""
length = backend._lib.EVP_PKEY_size(key._evp_pkey)
buffer = backend._ffi.new('unsigned char[]', length)
result = backend._lib.RSA_private_encrypt(
len(data), data, buffer,
backend._lib.EVP_PKEY_get1_RSA(key._evp_pkey),
backend._lib.RSA_PKCS1_PADDING)
backend.openssl_assert(result == length)
return backend._ffi.buffer(buffer)[:]
from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
privkey = load_pem_private_key(self.pri_key_str, None, backend)
tmp = openssl_private_encrypt(privkey, md5Str, backend)
来源:oschina
链接:https://my.oschina.net/u/584059/blog/2054473