- 首页 > it技术 > >
import base64
from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from config import conf# base64解密
def decode_base64(data):
missing_padding = 4 - len(data) % 4
if missing_padding:
data += b'=' * missing_padding
return base64.urlsafe_b64decode(data)# base64加密
def encode_base64(data):
data = https://www.it610.com/article/base64.urlsafe_b64encode(data)
return data# 公钥解密
def openssl_public_decrypt(key, data):
length = backend._lib.EVP_PKEY_size(key._evp_pkey)
buffer = backend._ffi.new('unsigned char[]', length)
backend._lib.RSA_public_decrypt(
len(data),
data,
buffer,
backend._lib.EVP_PKEY_get1_RSA(key._evp_pkey),
backend._lib.RSA_PKCS1_PADDING)
return backend._ffi.buffer(buffer)[:]def jdq_public_decrypt(data):
decrypt = ''
# base64解密
data_content = decode_base64(data.encode())
# 计算总长度
totalLen = len(data_content)
# 设置起点
decryptPos = 0
# 载入公钥
pubkey = load_pem_public_key(conf.otherPubKey.encode(), backend)while decryptPos < totalLen:
# 循环公钥解密
res = openssl_public_decrypt(pubkey, data_content[decryptPos:decryptPos + 128])
# 拼接
decrypt += res.decode()
# 起点+128
decryptPos += 128
return decrypt
推荐阅读