# -*- coding: utf-8 -*-
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as cipher_PKCS1_v1_5
from Crypto.Signature import PKCS1_v1_5 as signature_PKCS1_v1_5
import base64
def get_key():
rsa = RSA.generate(1024, Random.new().read)
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()
return {
"public_key": public_pem.decode(),
"private_key": private_pem.decode()
}
# 公钥加密
def rsa_encode(message, public_key):
rsakey = RSA.importKey(public_key)
cipher = cipher_PKCS1_v1_5.new(rsakey)
cipher_text = base64.b64encode(
cipher.encrypt(message.encode(encoding="utf-8")))
return cipher_text.decode()
# 私钥解密
def rsa_decode(cipher_text, private_key):
rsakey = RSA.importKey(private_key)
cipher = cipher
python利用pycrypto进行加密解密操作
最新推荐文章于 2024-08-08 20:06:38 发布