# coding:utf-8
import rsa
import random
import base64
import os
'''
RSA加密
在HR生成公钥私钥
在前端使用公钥加密,后端使用私钥解密
参考文档:https://blog.csdn.net/u012424148/article/details/109642169
经前端加密后,在服务端需要使用base64解码,再使用rsa解密
'''
def create_keys():
(pubkey, privkey) = rsa.newkeys(512)
print("公钥:\n%s\n私钥:\n%s" % (pubkey, privkey))
return (pubkey, privkey)
# rsa加密
def rsaEncrypt(str):
# 生成公钥、私钥
(pubkey, privkey) = rsa.newkeys(512)
print("公钥:\n%s\n私钥:\n%s" % (pubkey, privkey))
# 明文编码格式
content = str.encode("utf-8")
# 公钥加密
crypto = rsa.encrypt(content, pubkey)
return (crypto, privkey)
# rsa解密
def rsaDecrypt(str, pk):
# 私钥解密
# str
content = rsa.decrypt(str, pk)
con = content.decode("utf-8")
print("解密后明文:\n%s" % con)
return con
def save_keys(pubkey, privkey):
dirname, filename = os.path.split(os.path.abspath(__file__))
private_key_path = dirname + '/private.pem'
public_key_path = dirname + '/public.pem'
with open(public_key_path, 'w+') as f:
f.write(pubkey.save_pkcs1().decode())
print("save public key into public.pem")
with open(private_key_path, 'w+') as f:
f.write(privkey.save_pkcs1().decode())
print("save private key into private.pem")
def load_private_key():
"""绝对路径"""
dirname, filename = os.path.split(os.path.abspath(__file__))
# print(dirname) # 输出当前文件的绝对路径
private_key_path = dirname + '/private.pem'
with open(private_key_path, 'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
return privkey
def load_public_key():
"""绝对路径"""
dirname, filename = os.path.split(os.path.abspath(__file__))
# print dirname # 输出当前文件的绝对路径
public_key_path = dirname + '/public.pem'
with open(public_key_path, 'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
return pubkey
def load_keys():
pubkey = load_public_key()
privkey = load_private_key()
return pubkey, privkey
def rsaEncryptStr(str):
content = str.encode("utf-8")
# 公钥加密
pubkey =load_public_key()
crypto = base64.b64encode(rsa.encrypt(content, pubkey))
print("加密后密文:\n%s" % crypto)
return crypto
def rsaDecryptStr(str):
privkey = load_private_key()
content = rsaDecrypt(base64.b64decode(str), privkey)
return content
if __name__ == "__main__":
message = 'message-hello'
print('信息:%s'%message)
# pubkey, privkey = create_keys()
# save_keys(pubkey, privkey)
str = rsaEncryptStr(message)
content = rsaDecryptStr(str)
# pubkey, privkey = load_keys()
# content_1 = rsaDecrypt(str, privkey)
使用crypto库需要先安装第三方库pycryptodemo
# coding:utf-8
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto import Hash
import base64
from Crypto.Hash import SHA
from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher
def set_keys():
'''
生成密钥,保存密钥
:return:
'''
random_generator = Random.new().read
rsa = RSA.generate(2048, random_generator)
# 生成私钥
private_key = rsa.exportKey()
print(private_key.decode('utf-8'))
# 生成公钥
public_key = rsa.publickey().exportKey()
print(public_key.decode('utf-8'))
dirname, filename = os.path.split(os.path.abspath(__file__))
private_key_path = dirname + '/rsa_private_key.pem'
public_key_path = dirname + '/rsa_public_key.pem'
with open(private_key_path, 'wb')as f:
f.write(private_key)
with open(public_key_path, 'wb')as f:
f.write(public_key)
def get_key(key_file):
with open(key_file) as f:
data = f.read()
key = RSA.importKey(data)
return key
def encrypt_data(msg):
dirname, filename = os.path.split(os.path.abspath(__file__))
public_key_path = dirname + '/rsa_public_key.pem'
public_key = get_key(public_key_path)
cipher = PKCS1_cipher.new(public_key)
encrypt_text = base64.b64encode(cipher.encrypt(bytes(msg.encode("utf8"))))
return encrypt_text.decode('utf-8')
def decrypt_data(encrypt_msg):
dirname, filename = os.path.split(os.path.abspath(__file__))
private_key_path = dirname + '/rsa_private_key.pem'
private_key = get_key(private_key_path)
cipher = PKCS1_cipher.new(private_key)
back_text = cipher.decrypt(base64.b64decode(encrypt_msg), 0)
return back_text.decode('utf-8')
def test_encrypt_decrypt():
msg = "hellocrypto"
encrypt_text = encrypt_data(msg)
print("密文:%s"%encrypt_text)
decrypt_text = decrypt_data(encrypt_text)
print("明文:%s" % decrypt_text)
print(msg == decrypt_text)
if __name__ == "__main__":
# set_keys()
test_encrypt_decrypt()