RSA实际应用场景
import rsa # 生成密钥 pubkey, privkey = rsa.newkeys(512) # 保存密钥 print("==============保存密钥===============") with open('public.pem', 'w+') as f: f.write(pubkey.save_pkcs1().decode()) with open('private.pem', 'w+') as f: f.write(privkey.save_pkcs1().decode()) # 导入密钥 with open('public.pem', 'r') as f: pubkey = rsa.PublicKey.load_pkcs1(f.read().encode()) with open('private.pem', 'r') as f: privkey = rsa.PrivateKey.load_pkcs1(f.read().encode()) """ 加密 RSA """ def rsa_encrypt(message): crypto_email_text = rsa.encrypt(message.encode(), pubkey) return crypto_email_text text = rsa_encrypt("first test rsa") print(text) """ 解密 """ def rsa_decrypt(message): message_str = rsa.decrypt(message, privkey).decode() return message_str newmessage = rsa_encrypt("haha,one two three four smile!") message = rsa_decrypt(newmessage) print("\n", message) """ 签名 """ message = '这是重要指令:...' crypto_email_text = rsa.sign(message.encode(), privkey, 'SHA-1') """ 验证 """ # 收到指令明文、密文,然后用公钥验证,进行身份确认 rsa.verify(message.encode(), crypto_email_text, pubkey)