RSA加密使用OEAP及PKCS1_v1_5

import base64
from Crypto.Hash import SHA256, SHA1
from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5
from Crypto.Signature.pss import MGF1
from Crypto.PublicKey import RSA


def rsa_encrpyt_v15(text, publickey):
    text = bytes(text, encoding='utf-8')
    publickey = base64.b64decode(publickey)
    rsakey = RSA.importKey(publickey)
    cipher = Cipher_pkcs1_v1_5.new(rsakey)
    pkcs1_padding_text = cipher.encrypt(text)
    cipher_text = base64.b64encode(pkcs1_padding_text)
    return str(cipher_text, encoding='utf-8')


def rsa_encrpyt_oaep(text, publickey):
    """
    rsa加密
    Args:
        text: 被加密的明文
        publickey: 公钥

    Returns:加密结果

    """
    text = bytes(text, encoding='utf-8')
    publickey = base64.b64decode(publickey)
    public_key = RSA.importKey(publickey)
    cipher = PKCS1_OAEP.new(key=public_key, hashAlgo=SHA256, mgfunc=lambda x, y: MGF1(x, y, SHA1))
    pkcs1_padding_text = cipher.encrypt(text)
    cipher_text = base64.b64encode(pkcs1_padding_text)
    return str(cipher_text, encoding='utf-8')

之前也琢磨了很久oeap的加密,后来看到这篇文章,才完成加密:Pycrypto RSA PKCS1 OAEP SHA256与J的互操作性 - 问答 - Python中文网
上一篇:7_Attention(注意力机制)


下一篇:.MET Core RSA加密解密