rsa数据加密与解密

  • rsa的简单介绍
在公开密钥密码体制中,加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然解密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK
  • 算法的实现
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rsa
import base64


def text_encrypt(msg: str):
    """长数据加密"""
    msg = msg.encode('utf-8')
    length = len(msg)
    default_length = 245
    # 长度不用分段
    if length < default_length:
        return base64.b64encode(encrypt(msg))
    # 需要分段
    offset = 0
    res = []
    while length - offset > 0:
        if length - offset > default_length:
            res.append(encrypt(msg[offset:offset + default_length]))
        else:
            res.append(encrypt(msg[offset:]))
        offset += default_length
    byte_data = b''.join(res)
    return base64.b64encode(byte_data)


def text_decrypt(msg: bytes):
    """长数据解密"""
    msg = base64.b64decode(msg)
    length = len(msg)
    default_length = 256
    # 长度不用分段
    if length < default_length:
        return b''.join(decrypt(msg))
    # 需要分段
    offset = 0
    res = []
    while length - offset > 0:
        if length - offset > default_length:
            res.append(decrypt(msg[offset:offset + default_length]))
        else:
            res.append(decrypt(msg[offset:]))
        offset += default_length

    return b''.join(res).decode('utf8')


def decrypt(crypt_text: bytes):
    """
    用私钥解密--基础函数,直接执行时数据长度需要小于某个长度
    计算方法:len_in_byte(raw_data) = len_in_bit(key)/8
    """
    with open('private.pem', 'rb') as privatefile:
        p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)
    lase_text = rsa.decrypt(crypt_text, privkey)

    return lase_text


def encrypt(text: bytes):  # 用公钥加密
    """
    用公钥加密--基础函数,直接执行时数据长度需要小于某个长度
    计算方法:len_in_byte(raw_data) = len_in_bit(key)/8 -11
    """
    with open('public.pem', 'rb') as publickfile:
        p = publickfile.read()
    # print(p)
    pubkey = rsa.PublicKey.load_pkcs1(p)
    crypt_text = rsa.encrypt(text, pubkey)
    # print(type(crypt_text))
    return crypt_text  # 加密后的密文


def create_keys():  # 生成公钥和私钥
    (pubkey, privkey) = rsa.newkeys(2048)
    pub = pubkey.save_pkcs1()
    print(pub)
    with open('public.pem', 'wb+') as f:
        f.write(pub)

    pri = privkey.save_pkcs1()
    print(pri)
    with open('private.pem', 'wb+') as f:
        f.write(pri)
Last modification:May 14, 2022
If you think my article is useful to you, please feel free to appreciate