zoukankan      html  css  js  c++  java
  • python rsa模块【sign 加签验签】的使用

    https://www.cnblogs.com/kayb/p/8157556.html

    https://www.jianshu.com/p/518fa5d59f89

    **https://blog.csdn.net/ctwy291314/article/details/88822130

    公钥加密、私钥解密

     1 # -*- coding: utf-8 -*-
     2 import rsa
     3 
     4 # rsa加密
     5 def rsaEncrypt(str):
     6     # 生成公钥、私钥
     7     (pubkey, privkey) = rsa.newkeys(512)
     8     print("pub: ", pubkey)
     9     print("priv: ", privkey)
    10     # 明文编码格式
    11     content = str.encode('utf-8')
    12     # 公钥加密
    13     crypto = rsa.encrypt(content, pubkey)
    14     return (crypto, privkey)
    15 
    16 
    17 # rsa解密
    18 def rsaDecrypt(str, pk):
    19     # 私钥解密
    20     content = rsa.decrypt(str, pk)
    21     con = content.decode('utf-8')
    22     return con
    23 
    24 
    25 (a, b) = rsaEncrypt("hello")
    26 print('加密后密文:')
    27 print(a)
    28 content = rsaDecrypt(a, b)
    29 print('解密后明文:')
    30 print(content)  

    密钥导出、签名验证

     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', 'wb')
     9 pubfile.write(pub)
    10 pubfile.close()
    11 
    12 pri = privkey.save_pkcs1()
    13 prifile = open('private.pem', 'wb')
    14 prifile.write(pri)
    15 prifile.close()
    16 
    17 # load公钥和密钥
    18 message = 'lovesoo.org'
    19 with open('public.pem', "rb") as publickfile:
    20     p = publickfile.read()
    21     pubkey = rsa.PublicKey.load_pkcs1(p)
    22     print(pubkey)
    23 with open('private.pem', "rb") as privatefile:
    24     p = privatefile.read()
    25     privkey = rsa.PrivateKey.load_pkcs1(p)
    26     print(privkey)
    27 # 用公钥加密、再用私钥解密
    28 crypto = rsa.encrypt(message.encode('utf-8'), pubkey)
    29 message = rsa.decrypt(crypto, privkey)
    30 message = message.decode('utf-8')
    31 print (message)
    32 
    33 # sign 用私钥签名认证、再用公钥验证签名
    34 signature = rsa.sign(message.encode('utf-8'), privkey, 'SHA-1')
    35 method_name = rsa.verify('lovesoo.org'.encode('utf-8'), signature, pubkey)
    36 print(method_name)

    对文件进行RSA加密解密

    from rsa.bigfile import *
    import rsa
    
    with open('public.pem') as publickfile:
        p = publickfile.read()
        pubkey = rsa.PublicKey.load_pkcs1(p)
    
    with open('private.pem') as privatefile:
        p = privatefile.read()
        privkey = rsa.PrivateKey.load_pkcs1(p)
    with open('mysec.txt', 'rb') as infile, open('outputfile', 'wb') as outfile: #加密输出
        encrypt_bigfile(infile, outfile, pubkey)
    
    with open('outputfile', 'rb') as infile2, open('result', 'wb') as outfile2:  #解密输出
        decrypt_bigfile(infile2, outfile2, privkey)

    4.0的主要变化
    版本3.4是3.x范围内的最后一个版本。版本4.0删除了以下模块,因为它们不安全:

    rsa._version133
    rsa._version200
    rsa.bigfile
    rsa.varblock
    这些模块在3.4版中被标记为已弃用。

    此外,在4.0中,I / O函数经过简化,可以在所有支持的Python版本上使用字节。

    4.0版本不再支持Python 2.6和3.3。

  • 相关阅读:
    7,MongoDB 之 Limit 选取 Skip 跳过 Sort 排序
    Python的while else
    linux route
    NCO
    rand和randn
    vi常用操作
    Linux常用命令
    Jmeter的NON-GUI模式
    Linux下安装jdk&Jmeter
    MySql安装完成后,Navicat连接不上的问题
  • 原文地址:https://www.cnblogs.com/forforever/p/12870461.html
Copyright © 2011-2022 走看看