python RSA加密的示例
rsa加密是一种非对称加密,通常使用公钥加密,私钥解密,私钥签名,公钥验签。
在公开密钥密码*中,加密密钥(即公开密钥)pk是公开信息,而解密密钥(即秘密密钥)sk是需要保密的.rsa算法通常是先生成一对rsa密钥,其中之一是保密密钥,由用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。
rsa是一种公钥密码算法,加密算法是将明文m(m<n是一个整数)加密成密文c,即明文数字m的 e 次方求mod n,也就是将明文与自己相乘e次,然后结果除以n求余数,余数就是密文c,e和n组合就是公钥;解密算法为将密文c解密为明文m,即密文数字c的d次方求mod n,也就是将密文与自己相乘d次,然后结果除以n求余数,余数就是明文m,d和n组合就是私钥。
以下是关于rsa生成公钥私钥、加密、解密、加签、验签的示例。
1、生成公钥私钥:
from crypto import random from crypto.publickey import rsa # 伪随机数生成器 random_generator = random.new().read # rsa算法生成实例 rsa = rsa.generate(1024, random_generator) # 私钥的生成 private_pem = rsa.exportkey() with open("private.pem", "wb") as f: f.write(private_pem) # 公钥的生成 public_pem = rsa.publickey().exportkey() with open("public.pem", "wb") as f: f.write(public_pem)
生成的公钥私钥文件在项目路径下,也可以直接指定生成文件路径。
文件样例
生成的公钥私钥格式是固定的,秘钥中间无空格无换行,秘钥末尾也空格无换行,如下:
-----begin public key----- migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdeedv+5nsbqah6pjomkf8i7fga et3qmui0g5xdfqam219qqxnopi2hmcmjr8mwjv/zymz6iirg/pvrz2zhfdnfdw3z sfhczruvababzwar/57/edbjswv4rqa+gus6t8wfy/iv+o3i9+d79in3vhuogfi3 3ru3+rpfefw88tyuhwidaqab -----end public key----- -----begin rsa private key----- miicxaibaakbgqdeedv+5nsbqah6pjomkf8i7fgaet3qmui0g5xdfqam219qqxno pi2hmcmjr8mwjv/zymz6iirg/pvrz2zhfdnfdw3zsfhczruvababzwar/57/edbj swv4rqa+gus6t8wfy/iv+o3i9+d79in3vhuogfi33ru3+rpfefw88tyuhwidaqab aogaapzao5qag+giolroezor2/ueisjafupcg0acynt1llywgsoczv9qrqbwzk42 hmvf0gcznxmoj1eiben2pzkgveq/o4o8odhusk8pcdy72qwqhgsh1yfdvqkulo7d vdmjz63dvsrknyrqfyshiwupvvtwyt80otcysn3jgqnka9kcqqdzubtlbvcpzp4t 5hrah9xvch3ppusqkgpzvrvln+qz30n2pngttykrmshlz1wmts2zakr3b3blt6gt she5katfakea53jkuv1vuemg5j1clgrwts65ylwb+nblh84xekrkksxckvje7j/n qv7uk87na49luztbaecbmazytqnfkk7p2qjamzcc18lvbmbcnipr/49jjquwrohi gfo64nzzwphwix9h0dszccque7qjif1fhx0jxrywnjirv93rcvhu0mjunwjaigi/ jrxcc4sxpgnqc3gka5ca4cs/dfsp8cx8nlmwifx2k6d1nseeg5yjpaz9hul5f9of mtb3uroohyvwav1/uqjbaizqkryoojxpbfwviigboals/qqfe5klv+3l2rutblac njykmucrddx1gvkgoreuelxpytlwi17h9menxricgim= -----end rsa private key-----
2、加密(使用公钥加密)
from crypto.publickey import rsa from crypto.cipher import pkcs1_v1_5 as cipher_pkcs1_v1_5 import base64 # 加密 message = "hello,this is rsa加密" rsakey = rsa.importkey(open("public.pem").read()) cipher = cipher_pkcs1_v1_5.new(rsakey) #创建用于执行pkcs1_v1_5加密或解密的密码 cipher_text = base64.b64encode(cipher.encrypt(message.encode('utf-8'))) print(cipher_text.decode('utf-8'))
加密结果:
y1oivzbbdiewx+naxylcjo5a226tmuemketzmum3u80rw3gsetjg5rhq+s++yao+igq5jsja2yjkdtdajdvi2vuz15lrskdekoliwnwy93kkl+anesbl3suicatugfnwu5ilo+diltpf79afiehpptaz7+gn11kaf5ljfcqz2+0=
这里每次使用公钥加密后的结果都不一致,跟对数据的padding即填充有关。
加密时支持的最大字节数与证书有一定关系。加密时支持的最大字节数:证书位数/8 -11(比如:2048位的证书,支持的最大加密字节数:2048/8 - 11 = 245)
1024位的证书,加密时最大支持117个字节,解密时为128;
2048位的证书,加密时最大支持245个字节,解密时为256。
如果需要加密的字节数超出证书能加密的最大字节数,此时就需要进行分段加密。
3、解密(使用私钥解密)
from crypto.publickey import rsa from crypto.cipher import pkcs1_v1_5 as cipher_pkcs1_v1_5 import base64 # 解密 cipher_text = "y1oivzbbdiewx+naxylcjo5a226tmuemketzmum3u80rw3gsetjg5rhq+s++yao+igq5jsja2yjkdtdajdvi2vuz15lrskdekoliwnwy93kkl+anesbl3suicatugfnwu5ilo+diltpf79afiehpptaz7+gn11kaf5ljfcqz2+0=" encrypt_text = cipher_text.encode('utf-8') rsakey = rsa.importkey(open("private.pem").read()) cipher = cipher_pkcs1_v1_5.new(rsakey) #创建用于执行pkcs1_v1_5加密或解密的密码 text = cipher.decrypt(base64.b64decode(encrypt_text), "解密失败") print(text.decode('utf-8'))
解密结果,与加密前信息一致:
hello,this is rsa加密
4、加签(使用私钥加签)
使用私钥加签,每次签名是一致的。
from crypto.publickey import rsa from crypto.signature import pkcs1_v1_5 as signature_pkcs1_v1_5 from crypto.hash import sha import base64 #加签 message = "this is a request message..." rsakey = rsa.importkey(open("private.pem").read()) signer = signature_pkcs1_v1_5.new(rsakey) digest = sha.new() digest.update(message.encode("utf-8")) sign = signer.sign(digest) signature = base64.b64encode(sign) print(signature.decode('utf-8'))
签名结果:
fd99fqpbh48vt9yqkepyhsip9pwrjkm1pn3zykhnrftvk555fv392e7mtbifcligocwux8nd3g+7j0fo3x+9g1y6mjs0cumcba4qulumngjzupsn1urormzfpkjpvhf22arh9qzenebq7uugo3ioy4nylzonb3ldhga+pkyxytm=
5、验签(使用公钥验签)
from crypto.publickey import rsa from crypto.signature import pkcs1_v1_5 as signature_pkcs1_v1_5 from crypto.hash import sha import base64 #验签 message_verify = "this is a request message..." signature = "fd99fqpbh48vt9yqkepyhsip9pwrjkm1pn3zykhnrftvk555fv392e7mtbifcligocwux8nd3g+7j0fo3x+9g1y6mjs0cumcba4qulumngjzupsn1urormzfpkjpvhf22arh9qzenebq7uugo3ioy4nylzonb3ldhga+pkyxytm=" rsakey = rsa.importkey(open("public.pem").read()) verifier = signature_pkcs1_v1_5.new(rsakey) hsmsg = sha.new() hsmsg.update(message_verify.encode("utf-8")) is_verify = verifier.verify(hsmsg, base64.b64decode(signature)) print(is_verify)
验签结果:
true
以上就是python rsa加密的示例的详细内容,更多关于python rsa加密的资料请关注其它相关文章!
下一篇: Spring(狂神说)持续更新