zoukankan      html  css  js  c++  java
  • python RSA 非对称加解密的实现

    import rsa
    # key = rsa.newkeys(3000)#生成随机秘钥
    # print(key)
    # privateKey = key[1]#私钥
    # publicKey = key[0]#公钥
    publicKey,privateKey = rsa.newkeys(512)
    
    print("---------------------publickey-----------------------------------")
    print(publicKey)
    print("--------------------private----------------------------")
    print(privateKey)
    
    print("----------------------------------------------------------------")
    message ='今天是个好天气.'
    print('Before encrypted:',message)
    message = message.encode('utf-8')
    cryptedMessage = rsa.encrypt(message, publicKey)
    print('After encrypted:\n',cryptedMessage)
    message = rsa.decrypt(cryptedMessage, privateKey)
    message = message.decode('utf-8')
    print('After decrypted:',message)
    ---------------------publickey-----------------------------------
    PublicKey(11094339865497149601310490665695900299972958166379734275537930367526372009879675274379008260629160161421109910257865963868050787526283952661277229965514083, 65537)
    --------------------private----------------------------
    PrivateKey(11094339865497149601310490665695900299972958166379734275537930367526372009879675274379008260629160161421109910257865963868050787526283952661277229965514083, 65537, 11013930151349246187046449237705520016431032916077914329844955148572574504182831044365625229604162692550100530623607085898959541854732989355203187180984401, 6550078916062476789721607999392441084050668611994634378761780693113499854022840279, 1693771938883205994040350937493672750899574358189149966944230739222619477)
    ----------------------------------------------------------------
    Before encrypted: 今天是个好天气.
    After encrypted:
     b'\xb00Gj\x07|#\x94\xd2^\xf3\x1a\x1e\xdcB"\xf1~W\x1c\xcf\xf0\x9c\n62\xea\xf0\xc9Q\x11\xe7\x92\x9aP\x93\xb9\xc5\x98\xba\xf6\x9fz\xa4\x972~)\xd1\xb5u\x95`\xc9p\x18QZS\xae\xd75\x02\x1a'
    After decrypted: 今天是个好天气.
    
    Process finished with exit code 0
  • 相关阅读:
    10.22(day11) Object类 异常
    10.19(day10)
    10.18(day9)内部类 抽象类 接口
    10.17(day8) Static关键字 包的使用 访问修饰符的权限 设计模式
    paho-mqtt error1: incorrect protocol version解决方法
    Python进阶-pickle/eval/exec
    关联分析算法Apriori和FP-Growth
    LOF聚类分析
    Python进阶-迭代器和生成器
    Linux常见坑
  • 原文地址:https://www.cnblogs.com/zy09/p/15683427.html
Copyright © 2011-2022 走看看