python---rsa加密根据指数和模生成加密参数模板

2020-08-11 16:38:54 浏览数 (1)

代码 注释

代码语言:javascript复制
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import padding
import base64

"""
另种rsa加密
"""


def data_encrypt(text):
    """
        RSA 加密
    :param text:    加密前内容
    :return:        加密后内容
    """
    # 判断系统,加载指定模块
    public_exponent = int("010001",16)  #指数
    print(public_exponent)
    public_modulus=int('B23322F080BD5876C0735D585D25C7BC409F637237B07744D27FBF39FB100ABE59DF380EA6BFCDF28C286E7A0CD95BE87F6099F8F39B0E97D9782C3D33FCFB80D43D2F22A9D9417ECFD1A0B8421DEE1CD4B323E8078336E77419A97F94E60A90CA06551202F63819FC8E73425F06ECA4C05BBF8CA32366240A6C36CA61D85019',16) #模
    # content = 'leadeon'   text   time.strftime("%Y%m%d%H%M%S", time.localtime())
    content = text
    max_length = 117
    # public_key = serialization.load_pem_public_key(key, backend=default_backend())
    public_key = rsa.RSAPublicNumbers(public_exponent, public_modulus).public_key(default_backend())
    data = b''
    for i in range(0, len(content), max_length):
        data  = public_key.encrypt(content[i: i   max_length].encode(),
                                   padding.PKCS1v15())
    data = base64.b64encode(data).decode()
    return data

0 人点赞