• 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。

  • 相关阅读:
    今天特别忙
    代码重构十
    周末,悠哉的一天
    周六,游戏的一天
    代码重构九
    微信公众号网页上点击放大图片浏览,解决方案
    thinkphp 百度地图Api坐标计算 A坐标距离B坐标多少公里 并按照距离近的排序 坐标排序 外部字段排序
    php 中的关系运算符
    jquery 倒计时
    数组排序,
  • 原文地址:https://www.cnblogs.com/forforever/p/12870461.html
Copyright © 2020-2023  润新知