python3实现RSA/ECB/NOPADDING公钥加密

import rsa
import base64
from Crypto.PublicKey import RSA

def zfillStrToBin(s):
    b=bytes(s.encode())
    for i in range(128-len(b)):
        b+=b'\0'
    print(len(b))
    return b

class RsaNopadding:

    def __init__(self,key):
        self.pubkey = RSA.importKey(base64.b64decode(key))

    def encrypt(self, message):
        kLen = rsa.common.byte_size(self.pubkey.n)
        msg = zfillStrToBin(message)
        _b = rsa.transform.bytes2int(msg)
        _i = rsa.core.encrypt_int(_b, self.pubkey.e, self.pubkey.n)
        result = rsa.transform.int2bytes(_i, kLen)
        return result.hex().upper()

message='{"code":"123451","clienttime":1564560057}'
msg = RsaNopadding("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD2DT4odzkDd7hMlZ7djdZQH12j38nKxriINW1MGjMry3tXheya113xwmbBOwN0GA4zTwKFauFJRzcsD0nDFq1eaatcFKeDF25R4dnQRX+4BdTwFVS8lIb8nJMluSBwK+i4Z3VF+gfZ0AqQOXda6lJ4jPBt9Ep7VXEAHXUDn9JM8wIDAQAB")
print(msg.encrypt(message))

对应的java的  Cipher.getInstance("RSA/ECB/NOPADDING")加密,尝试了挺久的,一直没办法做到和java安卓的这个算法一致,参考了网上的一些资料,最终总结了上面的代码

你可能感兴趣的:(python,RSA)