python rsa和Crypto.PublicKey.RSA 模块
来源:脚本之家    时间:2022-04-21 18:01:25

文章目录 Crypto.PublicKey.RSA生成公私钥private.pempublic.pem 读取公私钥验证 使用公私钥加解密SHA256签名 rsa生成公私钥private.pempublic.pem 读取公私钥验证 使用公私钥加解密SHA256签名

rsa基础 https://blog.csdn.net/qq_52549196/article/details/123491446

Crypto.PublicKey.RSA

生成公私钥

import Crypto.PublicKey.RSAx = 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+CGb9lIkimJEwKXtRqAjCIAngiXZchKlnIAQBnAbI6VKQzJtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjnVPkOEbGHhvNc/BjW0x017B4p93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM5Al6HESniQ0gnhroSQIDAQABAoGASBRMNczKGReTHzPJcM4nVErveZJeQEshXhutd9HSjDrxutDhPoX0IoFADHwZirkty6ouM0Vv58b0il+oSyYLAGUS1Qrdr+kmOh8TjSeBt2xX+RKXwrzVY14MrsqJ3zOhlPcsyVDtGztX7+4Eel+mLElf2D41oB05pNiSTwBkE5sCQQC3H5c4GO/UReLZ4n/Fz0BMahAzgkXz+bLYZcJU+2chbuoStkFVEdtU3RvxbgaO34CJtXX2NaCfCr5rE0yGqPkbAkEA2kZLoGII0RCevNl0tzRatOwBZSdxuhh6emx1XMG3W4rOXifcB/jiA4bGME5ImOvhZ7v6mA/3VZX9FaPUMhb+awJAfJLiBUh5hldQflamA0P9Ar64bMH3ooRAFRQMglFQFB9m3vnbMerm4TFVfAUUMwd6YrPuHmLW9LqTVTYYvhae9wJBAIt12IIbwQha9yEwS0t2zvHDzMxlnFtLoo2v2HiwaMe7c0ljbElHspiQNvbMPcQ/CeCukVkw8lWjpHV8HGr4mAsCQC2ye3y2eaVqe7+tv8+DL1/idiKioY44uYtYIxNg1WxZXzig7Ri7DKOFRLw9wyS2OdBUpcZJ01xsIqtjWuHFMDM=-----END RSA PRIVATE KEY-----

public.pem

-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcIy+CGb9lIkimJEwKXtRqAjCIAngiXZchKlnIAQBnAbI6VKQzJtkrak2MQXVy9eN7tj97fTHCjRCxdRNP1VaROGjnVPkOEbGHhvNc/BjW0x017B4p93ncw8mbSM3f/+lzIVkdr9EA6iEOajh+hjehc6NM5Al6HESniQ0gnhroSQIDAQAB-----END PUBLIC KEY-----

读取公私钥

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

import Crypto.PublicKey.RSAwith 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_bytesc = bytes_to_long(b"xiaoxiaoran")  # 145568744200917766156476782e = 65537n = 109643441631386007615207297424331621014870648392454190718008050275074314522481791638636510984908444997607601077178481245842175926260005040415391800640503546886684365699706581166859202247619266556089792006189825410067873631530196098659194702332240479537964725096088707413180064673445928734211470673853181257801d = 50615797257072614971608239314043075331406747818629252377709912828588977962378890781255295811418169113327115473541607959661444856509045615381349027762609120393751592942681652318623050975912426954942311074907668935122731656262294557574124857501289932634444295689852337987042610580415469287093874225498684658587m = 4760371267684579630308571621455961776995122884329127227626895682331686504102393602160541535737343533151346614474308194637699485212151592440962407908438970696018720824027009690388552784020837086483711342994294618339728483898311036392512162634521480659231411043537783938261406684265246639310985133668266244632m = pow(c, d, n)  # m = c^d mod nc = pow(m, e, n)  # c = m^e mod n# print(m)  # 密文print(long_to_bytes(c))

使用公私钥加解密

import Crypto.PublicKey.RSAimport Crypto.Cipher.PKCS1_v1_5import Crypto.Randomstr = 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.Hashimport Crypto.Signature.PKCS1_v1_5import Crypto.PublicKey.RSAstr = 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)    msg_hash = Crypto.Hash.SHA256.new()    msg_hash.update(str)    verify = verifer.verify(msg_hash, sign)  # 使用公钥验证签名    print(verify)

rsa 生成公私钥

import rsapublic, 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/tP6VjhDQZW8hS4kA7RvSBUXpcJphXeOrU6M3DpyoEeIT17ZKWcI69jqJoH7J+35v8jb3y6kACVGKihQuYKhDU4T+QL6566Jc7g9Cxb4M+P9BLTVxY90X85cQ6mVQayZlUdyZP6bktoo/EclXc/d40Nt/+CApbFwIDAQABAoGBAIspymZabeH4LLvjBs5b4CC13LzeSO3LFbwcnDpjLMlg9YokS46Hd9Tk2H7Ru5q7q0c4dJwZmyffa/MUwC8TwPN5OHrdHm8JeHvZ7uOnHDW5KDcBCoiKIbCC7ViIzXxD5NkyKZ/Sof8IuWrQ1sgKXdg58gkE3DGpv2zZgFAt8+vhAkUA4JqTgtyrzU+Zv9IQGc4IdVNLA+WDMZkZy294zJYIc8bRB+5gm2cx6eZlqu2chTv0O16l4qa5jVkDqX7uHIBK6t0J2sMCPQCfJx/M6Th0XewFCQcxozEt6rNqXXPLl0kwkftUm/3U2+ZMw0DvG+rXbcKZH2aXsmhk8RPrCMTQJD9p8R0CRQCXcCHdriQ4fl9QMragb4bMdKizq+BVZME7paFxcAeq6HawPk3s+htxdeM+6I8MVmxKS3rV40xgvY/2jJrHpMjeqsKmOQI8XQk6/sdrOCNbgIoJiYvTiPsFjO06bs5ue5Uq7ggOd2thEoiloeGZA/xm4KpJh+5ENpXzRdcnbmFyLJVFAkRtWtGGcWZHUqf1zjXE8nJeSQyVNbmA+3bI8tKFsApAitohoV+pkRPH9sxjRQrBsCxXYIFb87IAEP4kiGPvMj7ZLHjlQw==-----END RSA PRIVATE KEY-----

public.pem

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

读取公私钥

import rsawith 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_bytesc = bytes_to_long(b"xiaoxiaoran")  # 145568744200917766156476782e = 65537n = 98054406985821460066911603492015436508222328814374109039765557792856783931929168543322947453890981964715321111180038798035875220954127258650951988858642038592569897228708251931577213665199206272026081143068775371001292827622859223963765601160574439687305128509021681199582745974505369805031375581229838129943d = 97723753706851312781030536852228210796512651309026340937811116969013438199763272236685866547116031829460395100444564354982852830185082869616332404661301340214981730355677936849258187423827926814158044656867718148132598580321985363744279619391251894170459217295515011494314707777862815537663422883333275904993m = 13886493648838710972140441482025058224090313231890577846399034902329916886695637737270404761882925259348133525185810448167361990827202097240482590034903652157080355065360915691138809161852288803959776946985306508273560998949496106417372434897047232906410711241839859082994232410306590851173666920567520980560m = pow(c, d, n)  # m = c^d mod nc = pow(m, e, n)  # c = m^e mod n# print(m)  # 密文print(long_to_bytes(c))

使用公私钥加解密

import rsastr = 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 rsastr = 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

关键词: 公钥加密

X 关闭

X 关闭