安装
pip install rsa
使用一
说明:简单入门使用 使用公钥加密 ----------->> 使用私钥进行解密
1 # -*- coding:utf-8 -*- 2 import rsa 3 4 5 def rsa_encrypt(d_str): 6 """ 7 生成私钥 和公钥 并接受字符串进行加密 8 :param d_str: 字符串 9 :return: 返回加密结果及私钥 10 """ 11 # 生成公钥和私钥 12 pubkey, privkey = rsa.newkeys(1024) 13 print pubkey, privkey 14 # 将字符串进行编码 15 content = d_str.encode(‘utf-8‘) 16 print ‘编码结果‘, content 17 # 公钥加密 18 crypto = rsa.encrypt(content, pubkey) 19 print ‘共钥加密结果‘, crypto 20 return crypto, privkey 21 22 23 def rsa_decrypt(crypto, privkey): 24 """ 25 解密 26 :param crypto: 公钥加密结果 27 :param privkey: 私钥 28 :return: 29 """ 30 # 解密 31 content = rsa.decrypt(crypto, privkey) 32 # 解码 33 content = content.decode(‘utf-8‘) 34 print ‘解密结果‘, content 35 36 37 if __name__ == ‘__main__‘: 38 a = rsa_encrypt(‘hello word‘) 39 rsa_decrypt(*a)
简单使用 公钥加密私钥解密Code
结果:
PublicKey(105532334855064558871471720144983044056346299365013755733259162501036790378738292019787069868554583994384472397785833870986231336954271316146541465051193038944174617155568148589694230338201718581390254372179722652044582086652863100420287539790025384495993582784568995192512904611505241844854221386155178376113, 65537) PrivateKey(105532334855064558871471720144983044056346299365013755733259162501036790378738292019787069868554583994384472397785833870986231336954271316146541465051193038944174617155568148589694230338201718581390254372179722652044582086652863100420287539790025384495993582784568995192512904611505241844854221386155178376113, 65537, 32731979959395414623501314606818596159930287123192007763400201354327705540818366142579180756185193537297300310203284329394649196427506188005412934223077799695642743507581779745970378384356510585976227244879763737251234324886626484049295170834651908035814868260255049993619457806756461841448318943841949636241, 43780083196928464550236946271732349312696781271723999154597463198120984522796589514105513242991062844313154180440124214418698597690847985597548059444175830087891717, 2410510148652904484317488108869831498999998903827157164423624876033568004965365613594737139603959496839915382306483478406548824172663536589804989) 编码结果 hello word 共钥加密结果 (???????ztJc3?d? ]@?d?/[email protected]??????B?C??[email protected]?0-???7=???teKH??u???ZJ???g??c?R??uAK??k=M9??=??‘??5:lt??F?f?,q?T?-?>[email protected] 解密结果 hello word
使用二
说明:可以直接生成公钥私钥并进行pkcs格式转换
# -*- coding:utf-8 -*- import rsa import base64 def generate_pubkey_privkey(): """ 生成公钥和私钥 :return: """ public_key, private_key = rsa.newkeys(1024) # 创建 print ‘公钥:‘, pub_pkcs = public_key.save_pkcs1() print pub_pkcs print ‘私钥:‘, priv_pkcs = private_key.save_pkcs1() print priv_pkcs # 假设以上得到的公钥及私钥 以下演示加密解密过程 mess = ‘hello word‘ print ‘加密前数据‘, mess # 加载公钥私钥 public_key = rsa.PublicKey.load_pkcs1(pub_pkcs) private_key = rsa.PrivateKey.load_pkcs1(priv_pkcs) print ‘加载到的公钥‘, print public_key print ‘加载到的私钥‘, print private_key result = rsa.encrypt(mess, public_key) print ‘加密结果‘, content = base64.encodestring(result) print content result = rsa.decrypt(base64.decodestring(content), private_key) print ‘解密结果‘, print result if __name__ == ‘__main__‘: generate_pubkey_privkey()
直接生成公钥私钥加密解密案例Code
结果:
公钥: -----BEGIN RSA PUBLIC KEY----- MIGJAoGBALti7dL+esfzMi6Z123+tZ/RkyznU7ugsrkLZuoyN+QOggU5pYsXH7bY deF7mQySmW5iMSuBpbBbpRxJ319J5UTKceLaO/L6ov3+eIovwlIHnGX5pd8QzlSV 3RUEWy8cwYistgDNBvDEa79pJRXlH/1O5W+lvzgNoHWH+/fmcgitAgMBAAE= -----END RSA PUBLIC KEY----- 私钥: -----BEGIN RSA PRIVATE KEY----- MIICYQIBAAKBgQC7Yu3S/nrH8zIumddt/rWf0ZMs51O7oLK5C2bqMjfkDoIFOaWL Fx+22HXhe5kMkpluYjErgaWwW6UcSd9fSeVEynHi2jvy+qL9/niKL8JSB5xl+aXf EM5Uld0VBFsvHMGIrLYAzQbwxGu/aSUV5R/9TuVvpb84DaB1h/v35nIIrQIDAQAB AoGBAK+Gurmy0LYtc2s9hhOtzNnYyWD9i2ys2d0F9jAYPExu3pIgbpX8YiO3XayK R1IEnJ5OcjqLpfWt0trNwzNeLBBm0pwkBipJvhB9PUXvebrSaSQ9XLTHOyCvtgZA rIUkvA06MwVD2UTvzxZgRDWwsQzaodPmIjMwvL0KbmhFqxoBAkUAvKO8KmCvqq7D Ou0ncJyBjPNznyD3ULAD5JugOW/s/MufX5ze+ovcJ/3+3zWJjsAmsRamyVFxFxxQ ci+nIYj6iQl9EYECPQD+TKOORE/64zX6gtO++Qdba1Bt3Q0uV58Pqgvr1oqjQx06 P6Z2G6DydYRDT2wn8VWtRO9xYgLek4F2dS0CRDg53YYMF5AaUxqnQVAxdfPbRKUz oEOukxp6wIEeOVcIIlLPYS7wrQhGjsddH8kqwN2b0M7EtZ6b0+aVCY3XVEWwTFUB Aj0AtfHLi7ygpNbwBf6Y1aiDdQtBDnn8Nk4GxkRaJzDnHIymWsizpwl5jEA8k5Q9 xA5NyJzr3GnNt8Y9+K05AkREFRpGMu71EZSDk7xlR6uGB5VSvqwLtdbP+6iabD22 btvhDbppOphVnEQ88tDh/1lllBMv/CEBrsyj7ZDN97faRRguIQ== -----END RSA PRIVATE KEY----- 加密前数据 hello word 加载到的公钥 PublicKey(131587234096741920779175642912883844559666214692610055570867202760447720434104757662756227268149385880941310691539830771748383700645819307242895140430520957795475529716183046516129032552194937247719617787028990120331064744900637723993694355860352548744617464179308616135644675215125455378152511253328774170797, 65537) 加载到的私钥 PrivateKey(131587234096741920779175642912883844559666214692610055570867202760447720434104757662756227268149385880941310691539830771748383700645819307242895140430520957795475529716183046516129032552194937247719617787028990120331064744900637723993694355860352548744617464179308616135644675215125455378152511253328774170797, 65537, 123258750232157251243004921537132098412703499607315542387353200638740331564295847660999771667369923094513116591283376890105765094510676433958437022917270147537132887314867013151363553679791541930382779949512171824652676794193139573303520889566550757298978628386029678563821858198700170484444076794153827768833, 42433662666258863461978927611251897319389836041413838112011034385202248177081665970527935082090355234493146174781377648518029689179287762497596881845967337929118081, 3101010514498281571972365602515998884303887562860511295373719676102937811331469329282673274291582789827470545355335918023460154317717884842177837) 加密结果 fbMjnlbTtWzK+rnQ+Fvx/Z8F7u+3bu5oXBMBG0fNOQ93jpE755Ir+EIeXvE/Rkg57y8VRobkFMDh OTHVfj1RZ2ZLlKj8Uphy1dA40Hr1AHZ4shjGxwlWivNMtfiAs6ksD0jLhABlwapTf6jl9mkSZw9W 7Kah+3ca/XMYNVKXNtU= 解密结果 hello word
结果Code
使用三
说明:加签 验签 眼签失败会抛出异常
1 # -*- coding:utf-8 -*- 2 import rsa 3 4 def jia_qian_demo(): 5 """ 6 加签验签 7 :return: 8 """ 9 public_key, private_key = rsa.newkeys(1024) # 创建 10 11 print ‘公钥:‘, 12 pub_pkcs = public_key.save_pkcs1() 13 print pub_pkcs 14 print ‘私钥:‘, 15 priv_pkcs = private_key.save_pkcs1() 16 print priv_pkcs 17 18 # 假设以上得到的公钥及私钥 一下演示加签验签 19 20 mess = ‘hello word‘ 21 print ‘加密前数据‘, mess 22 23 # 加载公钥私钥 24 public_key = rsa.PublicKey.load_pkcs1(pub_pkcs) 25 private_key = rsa.PrivateKey.load_pkcs1(priv_pkcs) 26 print ‘加载到的公钥‘, 27 print public_key 28 print ‘加载到的私钥‘, 29 print private_key 30 31 mess = ‘重要信息不可泄露‘ 32 result = rsa.sign(mess, private_key, ‘SHA-1‘) 33 print ‘加签结果‘, 34 print result 35 36 result = rsa.verify(mess, result, public_key) # 验签失败抛出异常 37 print ‘验签结果‘, 38 print result 39 40 if __name__ == ‘__main__‘: 41 jia_qian_demo()
加签 验签 案例Code
结果:
公钥: -----BEGIN RSA PUBLIC KEY----- MIGJAoGBAJVqkDkYlOgAY/y8bv/cCtxbob/mPFcF6JtiVuMObVR1hlYTiKiL2RTe yJLX5d2bGnhU3B59leVZ+oNcjuVS62MB9g+2riTiGN/x1DXeZjs2sEZO5VrFoBF2 U3lNPqRjsgzoFTVqcfUFr4O4IVC0qQiq0E/K6ArEMyB4hrlT9lqlAgMBAAE= -----END RSA PUBLIC KEY----- 私钥: -----BEGIN RSA PRIVATE KEY----- MIICYAIBAAKBgQCVapA5GJToAGP8vG7/3ArcW6G/5jxXBeibYlbjDm1UdYZWE4io i9kU3siS1+Xdmxp4VNwefZXlWfqDXI7lUutjAfYPtq4k4hjf8dQ13mY7NrBGTuVa xaARdlN5TT6kY7IM6BU1anH1Ba+DuCFQtKkIqtBPyugKxDMgeIa5U/ZapQIDAQAB AoGARX4vn+6X6yOrXXQCqEueXYjcvRUHQW5FFmfxg19FaD/VABnLuKB6kaNgZDO3 /I7UC91kF2ZPnkFwK0KT70nn39Pyq+CQgc5ycE3ehjStJ9j6Eh38wNg6eIbzFGld PhhOG5TGYTL12iI9AAReeMONXqYSCLq8l6oOzNEU3dN+Hs0CRQDO4kzfVbEXyc8s I3/q4N1zaMJhhalTYsAUA9fW2FYNT2PFYxe4yXzc5EEeljx/L3QhMrrMV+IaPmAJ PSnHzI+/x5g+KwI9ALjjkmSpLFp5+AjqZTaG0HCBNj5xPzsh3Hb5MXbyKIoaE8Im lUjA4STfvJpFvDPAe1P9VuiADdqc/e0ybwJFAKw95qhuLKHUiCKEb8R6cITcbDFL Wd0tsyM2y+V4ebRdK4Cz7pUPU2CIvNqdVELDnFjjZngolvsogUZP4NAStH+DynG/ AjwHGc5kiU7Hxsls5juu5WyZ9Yx0x3qrNbi25QkuTUybSTcbDMpM+gNrnCAtOKJq xmi2KBOvaEOf8smZ04cCRDlsLqgJIjnOaiIKuKNiuYzEwYYgnQzRq4AP2rjCJG5q ajyZSt1/6luiDN7RWCrusWtg3ashNTup1a6iYPLLpC6kc2F2 -----END RSA PRIVATE KEY----- 加密前数据 hello word 加载到的公钥 PublicKey(104923668177873610990710907595183022805319572154432933078574572974445308631826501249509331300185591353458547387639051456660698908681522353691041084812802642434220757729988444016144209396196921306050572227488695529623771584475563512960431878688822219498805751310959855252308213627500243245698594546289080687269, 65537) 加载到的私钥 PrivateKey(104923668177873610990710907595183022805319572154432933078574572974445308631826501249509331300185591353458547387639051456660698908681522353691041084812802642434220757729988444016144209396196921306050572227488695529623771584475563512960431878688822219498805751310959855252308213627500243245698594546289080687269, 65537, 48799583895048072640002733942792219938797105128999957782138815613074560208839336322783983511008392359198162609253184116613130955575041318077675540346134218507517161135334820935121227098159884944307100507857502687571800965611813875228035483634650919449141145666388314983843157846173454492436304190436299120333, 46537660807247476482343721332274423831646783587173026364563572911971952373945006999573837431392498419891616222098787387296980240166205914045002026387442240251772459, 2254596951326214329380524374466508855609079749515632320185208485662792835416443211707235676947903051873284825862592851258869281666513890246996591) 加签结果 [iěu??&T??Q$y????p?]\?nr????(???g:@??bH??
原文地址:https://www.cnblogs.com/kayb/p/8157556.html
时间: 2024-10-12 04:50:45