Loading... # 引言 公司项目对参数进行加密,但是在调试过程中需要解密才能知道参数以及结果的明文,故花费一小时来制作小工具。 # 代码 ``` import base64 import urllib.parse import json from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher from Crypto.PublicKey import RSA from Crypto import Random import hashlib from tkinter import * pubkey = """-----BEGIN RSA PUBLIC KEY----- MIICCgKCAgEAvbZ+f+7FOt/yj6qOXnG+28FdFKbvB3LGbTJqMq6S73ZAyt/wcHGO ZruhLqGK4/Ye4IIFg37KssQZuzDMS9mOpGTsyS+5uIhcTkxbjzHQt5ItDOzqFK5W St2A4rfQI42ONk+nnENx35wk3hD8fTez6JvWXcL3lKWW/c04PjBOnJXKT9Geuawp qsBiT+Knb6UHcCQIjDaPzNgDkfRhE0vRkWQ0dL0J6mBVlVN1+2UBhPxQZylA89dX Qd/A8CsLJnZJAhHWEvbsIGK5yjT+a5M8Br+9bhRQL80cDrKLpXejzzyTg9iCYrSb +cgcpTZqMUSag1vzZ3kjC89bn90hxQOeN95TqogrH2KsdIr+7+6xsgf7YsdOREW/ yu1rUjkAsoUvBLT76zz4D80SIfMbCnx/aIJw42UWLCDLJhg5iQaJMmKl4/jsrwLy GdEpht5rYlhnWWl5wN2UBSL0Qqe7EVRNEo7WI9MhwNtuYDQsttbWf5LaQn/jgUON OFLnposf4C245B3szS0r/BQUrEZaz9UnCdNueP/fdwZHcqUaBGutrshh+IMtXwxP GpdF3RyBhvtdWNzCIdbQp4KuGNdLdQ3ogcUbeyVkrkWlfo7UNBxfG66NfxOM6/eJ /91QsN7sweWoBUks/Kt/ipi1/a176qiE9hSWErw37E+8RZ54mpT3ZyECAwEAAQ== -----END RSA PUBLIC KEY----- """ privkey = """-----BEGIN RSA PRIVATE KEY----- MIIJNwIBAAKCAgEAvbZ+f+7FOt/yj6qOXnG+28FdFKbvB3LGbTJqMq6S73ZAyt/w cHGOZruhLqGK4/Ye4IIFg37KssQZuzDMS9mOpGTsyS+5uIhcTkxbjzHQt5ItDOzq FK5WSt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n7+N/yJem8PRjuSaQ7ndXlO6i71jqS2ki0vm vyeIwgsWQm/9h6x5ojegmpjGvjRVzd8oegnW8p2OFfYwRv1rHweBpo0NirbjgEEZ 4mkXD9IH/0/LsQ32YZZia8RS++kaabXZPF7wJS5YiI0yJQj0uI80Rxe8otDmPZ8k iTV5cDZkTiBPd6dvOnNPx/vkOQKB8CfHB2csRheOro57+KV5iJcBtsXX7nrjpS0d pwgM1AGge5SbHBO2+uXZzanP4jmJ/zzfg5PwF/T9R/WbOcg5gatweAYDyOxfDHnC qV7A0TH/41LIIJ0Z1DCZMcJ46J477+z1WFVuHE3ex+GLZifS0fVJhuCCdo1JJDiP 2FSEziSRvcok6g7Oy9kbXGq72vLp5Ek/dIKSmI4fMrngIHVCeVmbM79MLLKEbTS/ 3+LdLK6EPzJz/Rki36r+IV0p/JyXlCX4E2NCNDXu3s5lAX3ZyVtkXt4+QZqnlSCe pTs+RGpxcdaU1r7vAAn2jr2+ioax+QKCAREAggDat5H/BdxjS9wpgBcbWae/3/Zk rtEZq8/IQtCudmAVKqwWu0QvqUNCKn7kcXoTa/yqohlYJ+Uc/qS/wVE/6xw3t0DN mkWd21ZvayW5nUMlVDNvP1yzt9SxaFF6rg++ZIAwyud0X0IAAss9zekiHJnSf1mO 6iM0xwxcs8bSMy27iS7KWgc9UuwIlspCOdAhX/PSrqBzWZptZMQaAHzNzKaqlWwY NfsmW05d8Q04Azv8eQk1nsKpLZ5USd0MF9/t5r1kxOVdV8Z0RMRVCPAHFqZSwOrp c7jO8ZVjaU/XnzrK5NGINNeFujDfD6xgYI75CmLhZKlW9caxtCpX+m81mC6jtUp+ N/FTwA/U6oO1PEI= -----END RSA PRIVATE KEY-----""" # 非对称RSA加密 def encrypt(data, public_key=pubkey): if type(data) is str: data = data.encode('utf-8') pub_key = RSA.importKey(public_key) cipher = PKCS1_cipher.PKCS115_Cipher(pub_key, Random.get_random_bytes) keyLen = ((int(len(public_key) / 8)) - 11) if len(data) <= keyLen: rsa_text = base64.b64encode(cipher.encrypt(data)) return rsa_text else: start = 0 encrypt_data = b'' while start < len(data.decode('utf8')): end = start + keyLen data1 = data.decode('utf8')[start:end] encrypt_data += cipher.encrypt(data1.encode('utf8')) start = start + keyLen return base64.b64encode(encrypt_data) # 非对称RSA解密 def decrypt(data, private_key=privkey): if type(data) is str: data = data.encode('utf-8') pri_key = RSA.importKey(private_key) cipher = PKCS1_cipher.PKCS115_Cipher(pri_key, Random.get_random_bytes) binSec = base64.b64decode(data) keyLen = pri_key.size_in_bytes() if len(data) <= keyLen: rsa_text = cipher.decrypt(binSec, 0) return rsa_text else: start = 0 decrypt_data = b'' while start < len(binSec): end = start + keyLen data1 = binSec[start:end] decrypt_data += cipher.decrypt(data1, 0) start = start + keyLen return decrypt_data.decode('utf-8') def md5_encode(text): return hashlib.md5(text) def jiami(): text3.delete('1.0', 'end') text3.insert('insert', encrypt(text4.get('1.0', 'end'), text1.get('1.0', 'end'))) def jiemi(): text4.delete('1.0', 'end') decry = decrypt(text3.get('1.0', 'end'), text2.get('1.0', 'end')) unquote = urllib.parse.unquote(decry) try: json_dumps = json.dumps(json.loads(unquote), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) text4.insert('insert', json_dumps) except Exception as e: text4.insert('insert', unquote) if __name__ == '__main__': root = Tk() root.title('ZunMX - Crypto RSA') root.geometry('920x520') Label(root, text="公钥").grid(row=1, column=1) text1 = Text(root, width=65, height=15) text1.insert('insert', pubkey) text1.grid(row=2, column=1) Label(root, text="私钥").grid(row=1, column=2) text2 = Text(root, width=65, height=15) text2.insert('insert', privkey) text2.grid(row=2, column=2) Label(root, text="加密文本").grid(row=3, column=1) text3 = Text(root, width=65, height=18) text3.grid(row=4, column=1) Label(root, text="解密文本").grid(row=3, column=2) text4 = Text(root, width=65, height=18) text4.grid(row=4, column=2) b1 = Button(root, text="解密", width=50, command=jiemi) b2 = Button(root, text="加密", width=50, command=jiami) b1.grid(row=5, column=2) b2.grid(row=5, column=1) root.mainloop() ``` # 截图 ![image.png](https://www.zunmx.top/usr/uploads/2022/09/1699811335.png) © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏