python rsa 公钥解密_RSA私钥加密公钥解密实现(python3)

一、环境说明

1.操作系统:Ubuntu Desktop 18.04;

2.python版本:python3.6;

3.加密模块:M2Crypto

三、代码实现

# coding:utf-8

# Created:04/02/2019

# author:Radioman-lhq

import json

from M2Crypto import *

from base64 import b64encode, b64decode

def read_key(file_path, type):

"""

读取RSA密钥

:param file_path: 文件路径

:param type: 密钥类型,private:私钥|public:公钥

:return:

"""

with open(file_path, "rb") as file_handler:

rea_key = BIO.MemoryBuffer(file_handler.read())

if type == "private":

return RSA.load_key_bio(rea_key)

else:

return RSA.load_pub_key_bio(rea_key)

def hash(message, algorithm="md5"):

"""

计算散列值

:param message: 消息

:param algorithm: 散列算法

:return:

"""

hash = EVP.MessageDigest(algorithm)

hash.update(message.encode(encoding='utf-8'))

return hash.digest()

def sign(message, private_key, algorithm="md5"):

"""

RSA签名

:param message: 消息

:param private_key: 私

你可能感兴趣的:(python,rsa,公钥解密)