Python使用RSA库加密和解密

作者:springsnow 时间:2023-04-23 02:46:29 

一、rsa库(推荐)

1、公钥加密、私钥解密

# -*- coding: utf-8 -*-
import rsa

# rsa加密
def rsaEncrypt(str):
   # 生成公钥、私钥
   (pubkey, privkey) = rsa.newkeys(512)
   print("pub: ", pubkey)
   print("priv: ", privkey)
   # 明文编码格式
   content = str.encode('utf-8')
   # 公钥加密
   crypto = rsa.encrypt(content, pubkey)
   return (crypto, privkey)

# rsa解密
def rsaDecrypt(str, pk):
   # 私钥解密
   content = rsa.decrypt(str, pk)
   con = content.decode('utf-8')
   return con

(a, b) = rsaEncrypt("hello")
print('加密后密文:'+ a)
content = rsaDecrypt(a, b)
print('解密后明文:'+ content)

2、密钥导出、签名验证

import rsa

# 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用
(pubkey, privkey) = rsa.newkeys(1024)

pub = pubkey.save_pkcs1()
pubfile = open('public.pem', 'wb')
pubfile.write(pub)
pubfile.close()

pri = privkey.save_pkcs1()
prifile = open('private.pem', 'wb')
prifile.write(pri)
prifile.close()

# load公钥和密钥
message = 'lovesoo.org'
with open('public.pem', "rb") as publickfile:
   p = publickfile.read()
   pubkey = rsa.PublicKey.load_pkcs1(p)
   print(pubkey)

with open('private.pem', "rb") as privatefile:
   p = privatefile.read()
   privkey = rsa.PrivateKey.load_pkcs1(p)
   print(privkey)

# 用公钥加密、再用私钥解密
crypto = rsa.encrypt(message.encode('utf-8'), pubkey)
message = rsa.decrypt(crypto, privkey)
message = message.decode('utf-8')
print (message)

# sign 用私钥签名认证、再用公钥验证签名
signature = rsa.sign(message.encode('utf-8'), privkey, 'SHA-1')
verify = rsa.verify('lovesoo.org'.encode('utf-8'), signature, pubkey)
print(verify)

二、使用 Crypto.PublicKey.RSA库

1、使用 Crypto.PublicKey.RSA 生成公钥、私钥:

import Crypto.PublicKey.RSA
import Crypto.Random

x = Crypto.PublicKey.RSA.generate(2048)
#  Crypto.PublicKey.RSA.generate(2048, Crypto.Random.new().read)   使用 Crypto.Random.new().read 伪随机数生成器
a = x.exportKey("PEM")  # 生成私钥
b = x.publickey().exportKey()   # 生成公钥

with open("a.pem", "wb") as x:
   x.write(a)
with open("b.pem", "wb") as x:
   x.write(b)

2、使用 Crypto.PublicKey.RSA.importKey(private_key) 生成公钥和证书:

import Crypto.PublicKey.RSA

with open("a.pem", "rb") as x:
   xx = Crypto.PublicKey.RSA.importKey(x.read())

b = xx.publickey().exportKey()   # 生成公钥
with open("b.pem", "wb") as x:
   x.write(b)

a = xx.exportKey("DER")   # 生成 DER 格式的证书
with open("a.der", "wb") as x:
   x.write(a)

3、使用 Crypto进行RSA加解密

import Crypto.PublicKey.RSA
import Crypto.Cipher.PKCS1_v1_5
import Crypto.Random
import Crypto.Signature.PKCS1_v1_5
import Crypto.Hash

y = b"abcdefg1234567"

with open("b.pem", "rb") as x:
   b = x.read()
   cipher_public = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(b))
   cipher_text = cipher_public.encrypt(y) # 使用公钥进行加密

with open("a.pem", "rb") as x:
   a = x.read()
   # 如果私钥有密码 则使用相应密码 Crypto.PublicKey.RSA.importKey(a, password)
   cipher_private = Crypto.Cipher.PKCS1_v1_5.new(Crypto.PublicKey.RSA.importKey(a))
   text = cipher_private.decrypt(cipher_text, Crypto.Random.new().read)    # 使用私钥进行解密
assert text == y    # 断言验证

with open("c.pem", "rb") as x:
   c = x.read()
   c_rsa = Crypto.PublicKey.RSA.importKey(c)
   signer = Crypto.Signature.PKCS1_v1_5.new(c_rsa)
   msg_hash = Crypto.Hash.SHA256.new()
   msg_hash.update(y)
   sign = signer.sign(msg_hash)    # 使用私钥进行'sha256'签名

with open("d.pem", "rb") as x:
   d = x.read()
   d_rsa = Crypto.PublicKey.RSA.importKey(d)
   verifer = Crypto.Signature.PKCS1_v1_5.new(d_rsa)
   msg_hash = Crypto.Hash.SHA256.new()
   msg_hash.update(y)
   verify = verifer.verify(msg_hash, sign) # 使用公钥验证签名
   print(verify)

来源:https://www.cnblogs.com/springsnow/p/12582569.html

标签:Python,RSA,库,加密,解密
0
投稿

猜你喜欢

  • ASP编程菜鸟易犯的一个错误

    2008-10-29 13:27:00
  • SQL Server中的XML数据进行insert、update、delete

    2024-01-28 08:59:50
  • 详解MySQL Workbench使用教程

    2024-01-27 04:37:55
  • 2008年10佳改版网站

    2008-09-22 20:15:00
  • 详解Django将秒转换为xx天xx时xx分

    2023-06-14 22:52:12
  • 解析mysql与Oracle update的区别

    2024-01-25 01:35:55
  • Typora 1.4.8激活 2022最新Typora破解激活使用教程

    2022-02-13 18:14:59
  • Go 语言进阶freecache源码学习教程

    2023-08-06 03:05:20
  • 妄想or未来?界面的虚拟现实化

    2010-03-01 12:53:00
  • GDB调试Mysql实战之源码编译安装

    2024-01-28 00:37:42
  • python爬虫xpath模块简介示例代码

    2021-04-09 06:17:37
  • GoJs分组绘图模板go.Group使用示例详解

    2024-04-19 11:02:24
  • Vue 微信端扫描二维码苹果端却只能保存图片问题(解决方法)

    2024-05-02 16:58:01
  • Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)

    2021-06-11 17:17:54
  • python中argparse模块用法实例详解

    2022-01-09 23:34:40
  • python中format()函数的简单使用教程

    2021-07-10 15:34:01
  • python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例

    2022-01-26 01:33:47
  • 解析mysql 5.5字符集问题

    2024-01-13 09:01:54
  • 利用python做数据拟合详情

    2023-04-22 15:32:17
  • 关于MySQL与Golan分布式事务经典的七种解决方案

    2024-01-15 00:49:33
  • asp之家 网络编程 m.aspxhome.com