php的openssl_private_encrypt的python实现

原创
2018/09/18 11:20
阅读数 2K

参考:https://github.com/klokantech/flask-fastspring/blob/0462833f67727cba9755a26c88941f11f0159a48/flask_fastspring.py

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)
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部