1 Star 0 Fork 0

电子公文传输系统北方人North组合 / 信息安全系统设计与实现电子公文传输系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
decrypt.py 6.72 KB
一键复制 编辑 原始数据 按行查看 历史
吴龙灿 提交于 2022-12-12 03:07 . system
# -*- coding: UTF-8 -*-
# ! /usr/bin/env python
import base64
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_v1_5_cipper
from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
import rsa
from Cryptodome import Random
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Cryptodome.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
import Crypto
'''
# 使用 rsa库进行RSA签名和加解密
# 伪随机数生成器
random_generator = Random.new().read
with open('random.txt', 'w') as f:
f.write(str(random_generator))
# rsa算法生成实例
rsa = RSA.generate(1024, random_generator)
private_pem = str(rsa.exportKey(), encoding="utf-8")
with open("client-private.pem", "w") as f:
f.write(private_pem)
public_pem = str(rsa.publickey().exportKey(), encoding="utf-8")
with open("client-public.pem", "w") as f:
f.write(public_pem)
'''
class RsaUtil(object):
PUBLIC_KEY_PATH = 'client-public.pem' # 公钥
PRIVATE_KEY_PATH = 'client-private.pem' # 私钥
# 初始化key
def __init__(self,
company_pub_file=PUBLIC_KEY_PATH,
company_pri_file=PRIVATE_KEY_PATH):
if company_pub_file:
self.company_public_key = RSA.importKey(open(company_pub_file).read())
if company_pri_file:
self.company_private_key = RSA.importKey(open(company_pri_file).read())
def get_max_length(self, rsa_key, encrypt=True):
"""加密内容过长时 需要分段加密 换算每一段的长度.
:param rsa_key: 钥匙.
:param encrypt: 是否是加密.
"""
blocksize = Crypto.Util.number.size(rsa_key.n) / 8
reserve_size = 11 # 预留位为11
if not encrypt: # 解密时不需要考虑预留位
reserve_size = 0
maxlength = blocksize - reserve_size
return maxlength
# 加密 支付方公钥
def encrypt_by_public_key(self, encrypt_message):
"""使用公钥加密.
:param encrypt_message: 需要加密的内容.
加密之后需要对接过进行base64转码
"""
encrypt_result = b''
max_length = int(self.get_max_length(self.company_public_key))
cipher = PKCS1_v1_5_cipper.new(self.company_public_key)
while encrypt_message:
input_data = encrypt_message[:max_length]
encrypt_message = encrypt_message[max_length:]
out_data = cipher.encrypt(input_data)
encrypt_result += out_data
encrypt_result = base64.b64encode(encrypt_result)
return encrypt_result
# 加密 支付方私钥
def encrypt_by_private_key(self, encrypt_message):
"""使用私钥加密.
:param encrypt_message: 需要加密的内容.
加密之后需要对接过进行base64转码
"""
encrypt_result = b""
max_length = int(self.get_max_length(self.company_private_key))
cipher = PKCS1_v1_5_cipper.new(self.company_public_key)
while encrypt_message:
input_data = encrypt_message[:max_length]
encrypt_message = encrypt_message[max_length:]
out_data = cipher.encrypt(input_data.encode(encoding='utf-8').strip() + b"\n")
encrypt_result += out_data
encrypt_result = base64.b64encode(encrypt_result)
return encrypt_result
def decrypt_by_public_key(self, decrypt_message):
"""使用公钥解密.
:param decrypt_message: 需要解密的内容.
解密之后的内容直接是字符串,不需要在进行转义
"""
decrypt_result = b""
max_length = self.get_max_length(self.company_public_key, False)
decrypt_message = base64.b64decode(decrypt_message)
cipher = PKCS1_v1_5_cipper.new(self.company_public_key)
while decrypt_message:
input_data = decrypt_message[:max_length]
decrypt_message = decrypt_message[max_length:]
out_data = cipher.decrypt(input_data.encode(encoding='utf-8'), '')
decrypt_result += out_data
return decrypt_result
def decrypt_by_private_key(self, decrypt_message):
"""使用私钥解密.
:param decrypt_message: 需要解密的内容.
解密之后的内容直接是字符串,不需要在进行转义
"""
decrypt_result = b""
max_length = int(self.get_max_length(self.company_private_key, False))
decrypt_message = base64.b64decode(decrypt_message)
cipher = PKCS1_v1_5_cipper.new(self.company_private_key)
while decrypt_message:
input_data = decrypt_message[:max_length]
decrypt_message = decrypt_message[max_length:]
out_data = cipher.decrypt(input_data, '')
decrypt_result += out_data.strip() + b"\n"
#decrypt_result += str(out_data).encode(encoding='utf-8').strip() + b"\n"
return decrypt_result
# 签名 商户私钥 base64转码
def sign_by_private_key(self, message):
"""私钥签名.
:param message: 需要签名的内容.
签名之后,需要转义后输出
"""
cipher = PKCS1_v1_5.new(self.company_private_key) # 用公钥签名,会报错 raise TypeError("No private key") 如下
# if not self.has_private():
# raise TypeError("No private key")
hs = SHA.new(message)
signature = cipher.sign(hs)
return base64.b64encode(signature)
def verify_by_public_key(self, message, signature):
"""公钥验签.
:param message: 验签的内容.
:param signature: 对验签内容签名的值(签名之后,会进行b64encode转码,所以验签前也需转码).
"""
signature = base64.b64decode(signature)
cipher = PKCS1_v1_5.new(self.company_public_key)
hs = SHA.new(message)
# digest = hashlib.sha1(message).digest() # 内容摘要的生成方法有很多种,只要签名和解签用的是一样的就可以
return cipher.verify(hs, signature)
rsaUtil = RsaUtil()
'''encrypy_result = rsaUtil.encrypt_by_public_key(message)
with open(r'D:\document\study\RocEDU\dzgw\test4\cryed.txt',"wb") as w:
w.write(encrypy_result)
'''
with open(r'C:\Users\misaka\Desktop\cryed.docx','rb') as r:
encrypy_result = r.read()
decrypt_result = rsaUtil.decrypt_by_private_key(encrypy_result)
code =decrypt_result.decode(errors='ignore').encode('utf-8')
with open(r'C:\Users\misaka\Desktop\decry.docx', "wb") as w:
w.write(code)
with open(r'C:\Users\misaka\Desktop\1.docx', 'rb') as f:
message = f.read()
sign = rsaUtil.sign_by_private_key(bytearray(message))
print("验签结果:>>> ")
print(rsaUtil.verify_by_public_key(bytearray(message), sign))
1
https://gitee.com/north_4/dzgw.git
git@gitee.com:north_4/dzgw.git
north_4
dzgw
信息安全系统设计与实现电子公文传输系统
master

搜索帮助