Loading... # 引言 RSA是一种非对称加密算法,一般我们都使用公钥进行加密,私钥进行解密,当然,公钥和私钥都可以进行加解密,并且需要完整的公私钥,才能实现加解密流程,只有一个是无法进行解密的。 # 代码 pubkey和privkey修改成你自己生成的就好了。 ```python import base64 from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher from Crypto.PublicKey import RSA import rsa from Crypto import Random 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/Ye4IIFg37K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z1yJ/0iZmguKKq1k Wf/LNgL0ABLF8PtO1x3zCHgPy4gyn7+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 generate(): pubkey, privkey = rsa.newkeys(4096) print(pubkey.save_pkcs1()) print(privkey.save_pkcs1()) def encrypt(data, public_key): 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): 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') if __name__ == '__main__': for i in range(0, 1000): t = ('Security ' * i) e = encrypt(t, privkey) d = decrypt(e, privkey) print(d) ``` 如果想生成自己的密钥对,执行方法generate就好了,把输出的文本替换一下就可以使用了。 # 结论 RSA 加解密速度慢,但是安全性高,需要按需考虑。 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏