当前位置:主页 > 软件编程 > Python代码 >

python rsa和Crypto.PublicKey.RSA 模块详解

时间:2022-07-22 10:54:41 | 栏目:Python代码 | 点击:

rsa基础 https://www.jb51.net/article/245430.htm

Crypto.PublicKey.RSA

生成公私钥

import Crypto.PublicKey.RSA

x = Crypto.PublicKey.RSA.generate(1024)
private = x.exportKey("PEM")  # 生成私钥
public = x.publickey().exportKey()   # 生成公钥
with open("private.pem", "wb") as x:
    x.write(private)
with open("public.pem", "wb") as x:
    x.write(public)

生成的密钥结果

private.pem

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCcIy+CGb9lIkimJEwKXtRqAjCIAngiXZchKlnIAQBnAbI6VKQz
Jtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjnVPkOEbGHhvNc/BjW0x017B4p
93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM5Al6HESniQ0gnhroSQIDAQAB
AoGASBRMNczKGReTHzPJcM4nVErveZJeQEshXhutd9HSjDrxutDhPoX0IoFADHwZ
irkty6ouM0Vv58b0il+oSyYLAGUS1Qrdr+kmOh8TjSeBt2xX+RKXwrzVY14MrsqJ
3zOhlPcsyVDtGztX7+4Eel+mLElf2D41oB05pNiSTwBkE5sCQQC3H5c4GO/UReLZ
4n/Fz0BMahAzgkXz+bLYZcJU+2chbuoStkFVEdtU3RvxbgaO34CJtXX2NaCfCr5r
E0yGqPkbAkEA2kZLoGII0RCevNl0tzRatOwBZSdxuhh6emx1XMG3W4rOXifcB/ji
A4bGME5ImOvhZ7v6mA/3VZX9FaPUMhb+awJAfJLiBUh5hldQflamA0P9Ar64bMH3
ooRAFRQMglFQFB9m3vnbMerm4TFVfAUUMwd6YrPuHmLW9LqTVTYYvhae9wJBAIt1
2IIbwQha9yEwS0t2zvHDzMxlnFtLoo2v2HiwaMe7c0ljbElHspiQNvbMPcQ/CeCu
kVkw8lWjpHV8HGr4mAsCQC2ye3y2eaVqe7+tv8+DL1/idiKioY44uYtYIxNg1WxZ
Xzig7Ri7DKOFRLw9wyS2OdBUpcZJ01xsIqtjWuHFMDM=
-----END RSA PRIVATE KEY-----

public.pem

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcIy+CGb9lIkimJEwKXtRqAjCI
AngiXZchKlnIAQBnAbI6VKQzJtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjn
VPkOEbGHhvNc/BjW0x017B4p93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM
5Al6HESniQ0gnhroSQIDAQAB
-----END PUBLIC KEY-----

读取公私钥

公钥(n,e) 私钥(n,d)

import Crypto.PublicKey.RSA

with open("public.pem", 'rb') as x:
    public_key = Crypto.PublicKey.RSA.importKey(x.read())
    print(public_key.n)
    print(public_key.e)
with open("private.pem", 'rb') as x:
    private_key = Crypto.PublicKey.RSA.importKey(x.read())
    print(private_key.n)
    print(private_key.d)

验证

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = bytes_to_long(b'xiaoxiaoran')  # 145568744200917766156476782
e = 65537
n = 109643441631386007615207297424331621014870648392454190718008050275074314522481791638636510984908444997607601077178481245842175926260005040415391800640503546886684365699706581166859202247619266556089792006189825410067873631530196098659194702332240479537964725096088707413180064673445928734211470673853181257801
d = 50615797257072614971608239314043075331406747818629252377709912828588977962378890781255295811418169113327115473541607959661444856509045615381349027762609120393751592942681652318623050975912426954942311074907668935122731656262294557574124857501289932634444295689852337987042610580415469287093874225498684658587
m = 4760371267684579630308571621455961776995122884329127227626895682331686504102393602160541535737343533151346614474308194637699485212151592440962407908438970696018720824027009690388552784020837086483711342994294618339728483898311036392512162634521480659231411043537783938261406684265246639310985133668266244632
m = pow(c, d, n)  # m = c^d mod n
c = pow(m, e, n)  # c = m^e mod n
# print(m)  # 密文
print(long_to_bytes(c))

使用公私钥加解密

import Crypto.PublicKey.RSA
import Crypto.Cipher.PKCS1_v1_5
import Crypto.Random

str = b"xiaoxiaoran"
with open("./public.pem", "rb") as x:
    cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(x.read()))
    cipher_text = cipher_public.encrypt(str)  # 使用公钥进行加密
with open("./private.pem", "rb") as x:
    cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(x.read()))
    text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)    # 使用私钥进行解密
print(text)

SHA256签名

import Crypto.Hash
import Crypto.Signature.PKCS1_v1_5
import Crypto.PublicKey.RSA

str = b"xiaoxiaoran"
with open("./private.pem", "rb") as x:
    c_rsa = Crypto.PublicKey.RSA.importKey(x.read())
    signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
    msg_hash = Crypto.Hash.SHA256.new()
    msg_hash.update(str)
    sign = signer.sign(msg_hash)    # 使用私钥进行'sha256'签名
    # print(sign)
with open("./public.pem", "rb") as x:
    d_rsa = Crypto.PublicKey.RSA.importKey(x.read())
    verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
    verify = verifer.verify(msg_hash, sign)  # 使用公钥验证签名
    print(verify)

rsa

生成公私钥

import rsa

public, private = rsa.newkeys(1024)    # 生成公钥、私钥
with open("./private.pem", "wb") as x:  # 保存私钥
    x.write(private.save_pkcs1())
with open("./public.pem", "wb") as x:  # 保存公钥
    x.write(public.save_pkcs1())

private.pem

-----BEGIN RSA PRIVATE KEY-----
MIICYQIBAAKBgQCLolURP6bv3C/tP6VjhDQZW8hS4kA7RvSBUXpcJphXeOrU6M3D
pyoEeIT17ZKWcI69jqJoH7J+35v8jb3y6kACVGKihQuYKhDU4T+QL6566Jc7g9Cx
b4M+P9BLTVxY90X85cQ6mVQayZlUdyZP6bktoo/EclXc/d40Nt/+CApbFwIDAQAB
AoGBAIspymZabeH4LLvjBs5b4CC13LzeSO3LFbwcnDpjLMlg9YokS46Hd9Tk2H7R
u5q7q0c4dJwZmyffa/MUwC8TwPN5OHrdHm8JeHvZ7uOnHDW5KDcBCoiKIbCC7ViI
zXxD5NkyKZ/Sof8IuWrQ1sgKXdg58gkE3DGpv2zZgFAt8+vhAkUA4JqTgtyrzU+Z
v9IQGc4IdVNLA+WDMZkZy294zJYIc8bRB+5gm2cx6eZlqu2chTv0O16l4qa5jVkD
qX7uHIBK6t0J2sMCPQCfJx/M6Th0XewFCQcxozEt6rNqXXPLl0kwkftUm/3U2+ZM
w0DvG+rXbcKZH2aXsmhk8RPrCMTQJD9p8R0CRQCXcCHdriQ4fl9QMragb4bMdKiz
q+BVZME7paFxcAeq6HawPk3s+htxdeM+6I8MVmxKS3rV40xgvY/2jJrHpMjeqsKm
OQI8XQk6/sdrOCNbgIoJiYvTiPsFjO06bs5ue5Uq7ggOd2thEoiloeGZA/xm4KpJ
h+5ENpXzRdcnbmFyLJVFAkRtWtGGcWZHUqf1zjXE8nJeSQyVNbmA+3bI8tKFsApA
itohoV+pkRPH9sxjRQrBsCxXYIFb87IAEP4kiGPvMj7ZLHjlQw==
-----END RSA PRIVATE KEY-----

public.pem

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAIuiVRE/pu/cL+0/pWOENBlbyFLiQDtG9IFRelwmmFd46tTozcOnKgR4
hPXtkpZwjr2Oomgfsn7fm/yNvfLqQAJUYqKFC5gqENThP5AvrnrolzuD0LFvgz4/
0EtNXFj3RfzlxDqZVBrJmVR3Jk/puS2ij8RyVdz93jQ23/4IClsXAgMBAAE=
-----END RSA PUBLIC KEY-----

读取公私钥

import rsa

with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
    print(public_key.n)
    print(public_key.e)
with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
    print(private_key.n)
    print(private_key.d)

验证

公钥(n,e) 私钥(n,d)

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = bytes_to_long(b'xiaoxiaoran')  # 145568744200917766156476782
e = 65537
n = 98054406985821460066911603492015436508222328814374109039765557792856783931929168543322947453890981964715321111180038798035875220954127258650951988858642038592569897228708251931577213665199206272026081143068775371001292827622859223963765601160574439687305128509021681199582745974505369805031375581229838129943
d = 97723753706851312781030536852228210796512651309026340937811116969013438199763272236685866547116031829460395100444564354982852830185082869616332404661301340214981730355677936849258187423827926814158044656867718148132598580321985363744279619391251894170459217295515011494314707777862815537663422883333275904993
m = 13886493648838710972140441482025058224090313231890577846399034902329916886695637737270404761882925259348133525185810448167361990827202097240482590034903652157080355065360915691138809161852288803959776946985306508273560998949496106417372434897047232906410711241839859082994232410306590851173666920567520980560
m = pow(c, d, n)  # m = c^d mod n
c = pow(m, e, n)  # c = m^e mod n
# print(m)  # 密文
print(long_to_bytes(c))

使用公私钥加解密

import rsa

str = b"xiaoxiaoran"
with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
    cipher_text = rsa.encrypt(str, public_key)  # 使用公钥加密
with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
    text = rsa.decrypt(cipher_text, private_key)   # 使用私钥解密
print(text)

SHA256签名

import rsa

str = b"xiaoxiaoran"
with open("public.pem", 'rb') as x:
    public_key = rsa.PublicKey.load_pkcs1(x.read())
with open("private.pem", 'rb') as x:
    private_key = rsa.PrivateKey.load_pkcs1(x.read())
  
sign = rsa.sign(str, private_key, "SHA-256")  # 使用私钥进行'sha256'签名
verify = rsa.verify(str, sign, public_key)  # 使用公钥验证签名
print(verify)  # 签名正确则返回SHA-256

您可能感兴趣的文章:

相关文章