zoukankan      html  css  js  c++  java
  • python实现RSA加密解密方法

    python3.5 安装pip

    1 安装rsa
    2 python -m pip install rsa

    我们可以生成RSA公钥和密钥,也可以load一个.pem文件进来

     1 # -*- coding: utf-8 -*-
     2 import rsa
     3  
     4 # 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用
     5 (pubkey, privkey) = rsa.newkeys(1024)
     6  
     7 pub = pubkey.save_pkcs1()
     8 pubfile = open('public.pem','w+')
     9 pubfile.write(pub)
    10 pubfile.close()
    11  
    12 pri = privkey.save_pkcs1()
    13 prifile = open('private.pem','w+')
    14 prifile.write(pri)
    15 prifile.close()
    16  
    17 # load公钥和密钥
    18 message = 'lovesoo.org'
    19 with open('public.pem') as publickfile:
    20     p = publickfile.read()
    21     pubkey = rsa.PublicKey.load_pkcs1(p)
    22  
    23 with open('private.pem') as privatefile:
    24     p = privatefile.read()
    25     privkey = rsa.PrivateKey.load_pkcs1(p)
    26  
    27 # 用公钥加密、再用私钥解密
    28 crypto = rsa.encrypt(message, pubkey)
    29 message = rsa.decrypt(crypto, privkey)
    30 print message
    31  
    32 # sign 用私钥签名认证、再用公钥验证签名
    33 signature = rsa.sign(message, privkey, 'SHA-1')
    34 rsa.verify('lovesoo.org', signature, pubkey)

    持续更新

  • 相关阅读:
    9-1058. 选择题(20)
    8-素数打表
    7- 插入与归并
    6-爱丁顿数(题意理解)
    5-单身狗(时间和空间的相互选择)
    4-1068. 万绿丛中一点红
    3-1067. 试密码
    2-素数打比表
    21-矩形的嵌套
    maven设置打jar包并引入依赖包
  • 原文地址:https://www.cnblogs.com/wangshuyi/p/5768831.html
Copyright © 2011-2022 走看看