• python rsa-oaep加密示例


    代码:

     1 from Crypto.PublicKey import RSA
     2 from Crypto.Cipher import PKCS1_OAEP
     3 import base64
     4 rsa_key_pair = RSA.generate(1024)
     5 pubkey = rsa_key_pair.publickey().export_key()
     6 privkey = rsa_key_pair.export_key()
     7 print(pubkey.decode())
     8 print(privkey.decode())
     9 
    10 # 公钥加密
    11 text = "hello world"
    12 rsa_pubkey = RSA.import_key(pubkey)
    13 cipher_pub = PKCS1_OAEP.new(rsa_pubkey)
    14 ciphervalue_enc = base64.b64encode(cipher_pub.encrypt(text.encode("utf-8")))
    15 print("加密内容:
    {}".format(ciphervalue_enc.decode()))
    16 
    17 #私钥解密
    18 rsa_privkey = RSA.import_key(privkey)
    19 cipher_priv = PKCS1_OAEP.new(rsa_privkey)
    20 ciphervalue_dec = cipher_priv.decrypt(base64.b64decode(ciphervalue_enc))
    21 print("解密内容:
    {}".format(ciphervalue_dec.decode()))

    输出:

    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCk5RDTc88/13NClCz2u8JbERQZ
    5+5oqdic9LkqfpHXguxBd31j6sV9zBurnT85O6+08D/eRCZj7DA3vR2pXziglDaJ
    kUXNjuX0403NXHyNFugjbZyGfwr3r1V8B7ZCcyCZNW4V28naM0NF9jcVgb6haDQP
    7k5ht92sQ7IJiXROKQIDAQAB
    -----END PUBLIC KEY-----
    -----BEGIN RSA PRIVATE KEY-----
    MIICXQIBAAKBgQCk5RDTc88/13NClCz2u8JbERQZ5+5oqdic9LkqfpHXguxBd31j
    6sV9zBurnT85O6+08D/eRCZj7DA3vR2pXziglDaJkUXNjuX0403NXHyNFugjbZyG
    fwr3r1V8B7ZCcyCZNW4V28naM0NF9jcVgb6haDQP7k5ht92sQ7IJiXROKQIDAQAB
    AoGARVXAVnlayuhoIbW2lmTq22n5/s6kU8QfPhg1B+ZCotD9UKXSxsHAGx5JAEdV
    f1ZWkol3AZm8XsXAnJ9bqo1MfKQcPLZRCG3FMQ6UUyse/R0Wc+ofzP8roM6y9qsG
    C8WlM5lSojyz+lgUH5+CaEtZW43kHitt/jzK0byj2An0c1UCQQDH6kDFh43mLFz0
    n4q6VSnCrCGTTuEsnnWGSlNafFyUURgeA6AWkfJNphErNBJul/thq6lXf9Hs61sd
    CI1WtDDTAkEA0yevHkbD4hV9eh4aW5r6sAgDEtpp3gaUoB9xyayeuO1GoowtTBhz
    NO+GttV+XyxFELCNBBwNIhpvVFWz/q+HkwJBAMGLuIPEtgum3aPzpiK5+sE/Vw4/
    6t8A1rVZ1hMfnZ32WQwN5HqVd+nXIn4aXtH1kGnCASP2Wu4t+jtEoVnVAAMCQH9c
    zmnqsThrUfYYtjlO76rCCmiSyYycV4WFsfK9ZjeRFyvwdVoS3Vqup4kcmU76uuXo
    OAakjN9nJoOt9hXG9nUCQQCJc398MvDedu8igFluMx8Cgt6s3Srwp/EZvcBK915u
    e5JvZ6uds64G9ooX+8mire50zpn7UbwPZzbEDffNYbIG
    -----END RSA PRIVATE KEY-----
    加密内容:
    Bod1WNuWMRky/bCZdNvWuCBYdMfejsEWqm08QwNxff3enwm1xxW6z/jEhs3trT8dmKgvX20wYSccH/7Ekcc/09Jnd6N3AuiCE922U5OdFLqOFhkORdQZP6uWisiiW7XoYNoY/Hzr8ZH+7s5/sw2w0MIpRyD+4Wlooxd8UDgThWA=
    解密内容:
    hello world

     RSA.import_Key()参数可以是bytes或string, 支持X509标准的公钥证书或者PKCS#1 DER编码的私钥证书,详细内容可见函数内的说明。

  • 相关阅读:
    http响应状态码大全
    Oracle中start with...connect by子句的用法
    sql的连接查询
    spring框架等web程序在tomcat下的启动顺序
    rest模式get,post,put,delete简单讲解
    context-param和init-param的区别
    spring的webutils包。适用于访问httpservletrequest和httpservletresponse
    java日志的相关配置文件知识
    jquery开发插件提供的几种方法
    Node.js实现网络编程
  • 原文地址:https://www.cnblogs.com/brian-sun/p/13559073.html
Copyright © 2020-2023  润新知