您的位置:首页技术文章
文章详情页

python rsa-oaep加密的示例代码

【字号: 日期:2022-07-10 11:18:12浏览:3作者:猪猪

代码:

from Crypto.PublicKey import RSAfrom Crypto.Cipher import PKCS1_OAEPimport base64rsa_key_pair = RSA.generate(1024)pubkey = rsa_key_pair.publickey().export_key()privkey = rsa_key_pair.export_key()print(pubkey.decode())print(privkey.decode())# 公钥加密text = 'hello world'rsa_pubkey = RSA.import_key(pubkey)cipher_pub = PKCS1_OAEP.new(rsa_pubkey)ciphervalue_enc = base64.b64encode(cipher_pub.encrypt(text.encode('utf-8')))print('加密内容:n{}'.format(ciphervalue_enc.decode()))#私钥解密rsa_privkey = RSA.import_key(privkey)cipher_priv = PKCS1_OAEP.new(rsa_privkey)ciphervalue_dec = cipher_priv.decrypt(base64.b64decode(ciphervalue_enc))print('解密内容:n{}'.format(ciphervalue_dec.decode()))

输出:

-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCk5RDTc88/13NClCz2u8JbERQZ5+5oqdic9LkqfpHXguxBd31j6sV9zBurnT85O6+08D/eRCZj7DA3vR2pXziglDaJkUXNjuX0403NXHyNFugjbZyGfwr3r1V8B7ZCcyCZNW4V28naM0NF9jcVgb6haDQP7k5ht92sQ7IJiXROKQIDAQAB-----END PUBLIC KEY----------BEGIN RSA PRIVATE KEY-----MIICXQIBAAKBgQCk5RDTc88/13NClCz2u8JbERQZ5+5oqdic9LkqfpHXguxBd31j6sV9zBurnT85O6+08D/eRCZj7DA3vR2pXziglDaJkUXNjuX0403NXHyNFugjbZyGfwr3r1V8B7ZCcyCZNW4V28naM0NF9jcVgb6haDQP7k5ht92sQ7IJiXROKQIDAQABAoGARVXAVnlayuhoIbW2lmTq22n5/s6kU8QfPhg1B+ZCotD9UKXSxsHAGx5JAEdVf1ZWkol3AZm8XsXAnJ9bqo1MfKQcPLZRCG3FMQ6UUyse/R0Wc+ofzP8roM6y9qsGC8WlM5lSojyz+lgUH5+CaEtZW43kHitt/jzK0byj2An0c1UCQQDH6kDFh43mLFz0n4q6VSnCrCGTTuEsnnWGSlNafFyUURgeA6AWkfJNphErNBJul/thq6lXf9Hs61sdCI1WtDDTAkEA0yevHkbD4hV9eh4aW5r6sAgDEtpp3gaUoB9xyayeuO1GoowtTBhzNO+GttV+XyxFELCNBBwNIhpvVFWz/q+HkwJBAMGLuIPEtgum3aPzpiK5+sE/Vw4/6t8A1rVZ1hMfnZ32WQwN5HqVd+nXIn4aXtH1kGnCASP2Wu4t+jtEoVnVAAMCQH9czmnqsThrUfYYtjlO76rCCmiSyYycV4WFsfK9ZjeRFyvwdVoS3Vqup4kcmU76uuXoOAakjN9nJoOt9hXG9nUCQQCJc398MvDedu8igFluMx8Cgt6s3Srwp/EZvcBK915ue5JvZ6uds64G9ooX+8mire50zpn7UbwPZzbEDffNYbIG-----END RSA PRIVATE KEY-----加密内容:Bod1WNuWMRky/bCZdNvWuCBYdMfejsEWqm08QwNxff3enwm1xxW6z/jEhs3trT8dmKgvX20wYSccH/7Ekcc/09Jnd6N3AuiCE922U5OdFLqOFhkORdQZP6uWisiiW7XoYNoY/Hzr8ZH+7s5/sw2w0MIpRyD+4Wlooxd8UDgThWA=解密内容:hello world

以上就是python rsa-oaep加密示例的示例代码的详细内容,更多关于python rsa-oaep加密的资料请关注好吧啦网其它相关文章!

标签: Python 编程
相关文章: