python简单的加密解密
rsa 是非对称加密
公钥加密,私钥解密
pip install rsa
import rsa from binascii import b2a_hex, a2b_hex class rsacrypt(): def __init__(self): '''''' def encrypt(self, text): #加密文本 self.ciphertext = rsa.encrypt(text.encode(),self.pubkey) return b2a_hex(self.ciphertext) def decrypt(self, text): #解密文本 decrypt_text = rsa.decrypt(a2b_hex(text),self.prikey) return decrypt_text def create_key(self): #获取一对秘钥 pubkey, prikey = rsa.newkeys(256) pub = pubkey.save_pkcs1() pri = prikey.save_pkcs1() return (pub,pri) def load_pub_key(self,pub): #导入公钥,为加密准备 self.pubkey = rsa.PublicKey.load_pkcs1(pub) def load_pri_key(self,pri): #导入私钥,为解密准备 self.prikey = rsa.PrivateKey.load_pkcs1(pri) if __name__ == '__main__': my_rsacrypt = rsacrypt() pub,pri=my_rsacrypt.create_key() print(pub,pri) my_rsacrypt.load_pub_key(pub) my_rsacrypt.load_pri_key(pri) a = my_rsacrypt.encrypt("123") print(a) b = my_rsacrypt.decrypt(a) print(b)
b'5e1ed0b7583e9cdb3f9161fd6e045c29458f703cabb91a848a84e95cab35a113'
b'123'