Python RSA加密
Mac上使用生成RSA公钥和私钥
安装命令如下:
sudo apt-get install openssl
在你的任何地方创建一个Folder来保存你的公钥和密钥,
打开终端Terminal,cd命令进入到你创建的Folder中,比如在桌面新建一个”RSA公钥私钥“文件夹,进入这个文件夹,命令如图:
接下来我们开始生成公钥和密钥:
1.生成RSA私钥:
在Terminal中输入下面命令(该命令会生成1024位的私钥):
openssl genrsa -out rsa_private_key.pem 1024
回车后如下图显示:
此时你可以在”RSA公钥私钥“文件夹下看到rsa_private_key.pem文件了。
2.把RSA私钥转换成PKCS8格式
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM –nocrypt
3.生成公钥:
openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout
此时,我们可以看到一个文件名为rsa_public_key.pem的文件,这个就是公钥。
到此,文件夹“RSA公钥私钥”下面就会有两个文件:rsa_private_key.pem 和 rsa_public_key.pem,就是我们需要的公钥和私钥!
方法一:load 公钥和私钥 (导入公私玥)
# -*- coding: utf-8 -*-
import rsa
#load 公钥和私钥
with open('./rsa_public_key.pem' ,'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('./rsa_private_key.pem' ,'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
"""
加密 RSA
def rsa_encrypt(message):
crypto_email_text = rsa.encrypt(message.encode(), pubkey)
return crypto_email_text
# message = "这是商机:..."
# print(pubkey,"\n")
# crypto_email_text = rsa.encrypt(message.encode(), pubkey)
# print(crypto_email_text)
text = rsa_encrypt("hello world")
print(text)
"""
解密
"""
def rsa_decrypt(message):
message_str = rsa.decrypt(message,privkey).decode()
return message_str
message = rsa.decrypt(crypto_email_text, privkey).decode()
print("\n",message)
"""
签名
"""
message = '这是重要指令:...'
# # 公司经理私钥签名
crypto_email_text = rsa.sign(message.encode(), privkey, 'SHA-1')
"""
验证
"""
# 业务员同时收到指令明文、密文,然后用公钥验证,进行身份确认
rsa.verify(message.encode(), crypto_email_text, pubkey)
运行时报错
ValueError: No PEM start marker "-----BEGIN RSA PUBLIC KEY-----" found
解决方案:
openssl rsa -in rsa_public_key.pem -RSAPublicKey_out -out pubkey.pem
方法二:生成公私玥
# -*- coding: utf-8 -*-
import rsa
# 生成密钥
(pubkey, privkey) = rsa.newkeys(1024)
# =================================
# 场景〇:密钥保存导入
# =================================
# 保存密钥
with open('public.pem' ,'w+') as f:
f.write(pubkey.save_pkcs1().decode())
print("=============================")
with open('private.pem' ,'w+') as f:
f.write(privkey.save_pkcs1().decode())
#导入密钥
with open('public.pem' ,'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('private.pem' ,'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
with open('./rsa_public_key.pem' ,'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('./rsa_private_key.pem' ,'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode()
"""
加密 RSA
"""
def rsa_encrypt(message):
crypto_email_text = rsa.encrypt(message.encode(), pubkey)
return crypto_email_text
# message = "这是商机:..."
# print(pubkey,"\n")
# crypto_email_text = rsa.encrypt(message.encode(), pubkey)
# print(crypto_email_text)
text = rsa_encrypt("hello world")
print(text)
"""
解密
"""
def rsa_decrypt(message):
message_str = rsa.decrypt(message,privkey).decode()
return message_str
message = rsa.decrypt(crypto_email_text, privkey).decode()
print("\n",message)
"""
签名
"""
message = '这是重要指令:...'
crypto_email_text = rsa.sign(message.encode(), privkey, 'SHA-1')
"""
验证
"""
# 收到指令明文、密文,然后用公钥验证,进行身份确认
rsa.verify(message.encode(), crypto_email_text, pubkey)