python RSA加密最新(RSA/ECB/PKCS1Padding)

遇到一个 java 的RSA/ECB/PKCS1Padding 加密 要改成python的


网上搜一堆不靠谱的,没办法只能自己动手写一份了


中间遇到一个ERROR:

 OverflowError: 458 bytes needed for message, but there is only space for 117

遇到问题解决它

上代码:

import rsa
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
#rsa加密,通常对加密结果进行base64编码

def handle_pub_key(key):
    """
    处理公钥
    公钥格式pem,处理成以-----BEGIN PUBLIC KEY-----开头,-----END PUBLIC KEY-----结尾的格式
    :param key:pem格式的公钥,无-----BEGIN PUBLIC KEY-----开头,-----END PUBLIC KEY-----结尾
    :return:
    """
    start = '-----BEGIN PUBLIC KEY-----\n'
    end = '-----END PUBLIC KEY-----'
    result = ''
    # 分割key,每64位长度换一行
    divide = int(len(key) / 64)
    divide = divide if (divide > 0) else divide + 1
    line = divide if (len(key) % 64 == 0) else divide + 1
    for i in range(line):
        result += key[i * 64:(i + 1) * 64] + '\n'
    result = start + result + end
    return result


def get_param(message, public_key):
    """
    处理长消息 不经过 这个处理回报下面error
    OverflowError: 458 bytes needed for message, but there is only space for 117
    :param message 消息
    :param public_key 公钥
    :return:
    """
    pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(public_key)
    crypto = b''
    divide = int(len(message) / 117)
    divide = divide if (divide > 0) else divide + 1
    line = divide if (len(message) % 117 == 0) else divide + 1
    for i in range(line):
        crypto += rsa.encrypt(message[i * 117:(i + 1) * 117].encode(), pubkey)

    crypto1 = base64.b64encode(crypto)
    return crypto1.decode()



if __name__ == '__main__':
    message = "infodownloadermiddlewareshttpcompressionHttpCompressionMiddleware"*10
    public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClsqQiK5KMBO88nf2CE6I5aGJQX9jTXorDveudfSKts2/5i/ipCLo68rl4gsPYwzjP5ef5IJTK0Xdzrrfkn5d2GCVA7n/jN3rlqjfSy1w2D4JqMUtqEhRQr7KfofZbZBnPOooiepRht+W0D9rIAceLLD5UPpstZ4lPCW2t/PG0hQIDAQAB"
    public_key = handle_pub_key(public_key)
    param = get_param(message, public_key)
    print(param)

success!

用了记得点赞!留个言更好!

你可能感兴趣的:(安卓逆向,Spider)