其他分享
首页 > 其他分享> > RSA加密使用OEAP及PKCS1_v1_5

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中文网

标签:v1,text,base64,RSA,publickey,cipher,import,PKCS1
来源: https://blog.csdn.net/ouyanglin111/article/details/120224617