教学服务系统

 找回密码
 立即注册
搜索
查看: 672|回复: 0

信息计算2019级1班2号孙志慧

[复制链接]

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
发表于 2022-4-8 16:41:04 | 显示全部楼层 |阅读模式
本帖最后由 孙志慧 于 2022-4-8 16:49 编辑

解密过程:
  1. from secrets import token_bytes
  2. from docx import Document
  3. import docx
  4. import time
  5. def random_key(length):
  6.     key = token_bytes(nbytes=length)
  7.     key_int = int.from_bytes(key, 'big')
  8.     return key_int
  9. def encrypt(raw):
  10.     raw_bytes = raw.encode()
  11.     raw_int = int.from_bytes(raw_bytes, 'big')
  12.     key_int = random_key(len(raw_bytes))
  13.     return raw_int ^ key_int, key_int
  14. def decrypt(encrypted, key_int):
  15.     decrypted = encrypted ^ key_int
  16.     length = (decrypted.bit_length() + 7) // 8
  17.     decrypted_bytes = int.to_bytes(decrypted, length, 'big')
  18.     return decrypted_bytes.decode()
  19. s = []
  20. h = []
  21. def decrypt_file(path_encrypted, key_path=None, *, encoding='utf-8'):
  22.     document = Document(path_encrypted)
  23.     all_paragraphs = document.paragraphs
  24.     do2 = Document('key.docx')
  25.     all_p= do2.paragraphs
  26.     for i in all_paragraphs:
  27.         #str转int
  28.         jiam = int(i.text)
  29.         s.append(jiam)
  30.     for k in all_p:
  31.         key = int(k.text)
  32.         h.append(key)
  33.     c = zip(s,h)
  34.     res = list(c)
  35.     return res
  36. print('禁止修改密钥文件名')
  37. rr1 = decrypt_file(input("输入要破解文件名")+'.docx')
  38. file = docx.Document()
  39. for i in rr1:
  40.     f = decrypt(*i)
  41.     file.add_paragraph(f)
  42. file.save("res.docx")
  43. print('解密完成!')
复制代码


加密过程:
  1. from secrets import token_bytes
  2. from doc import Document
  3. import doc
  4. import time
  5. def random_key(length):
  6.     # token_bytes,函数接受一个int参数,用于指定随机字节串的长度。
  7.     # int.from_bytes把字节串转换为int,也就是我们需要的二进制数
  8.     key = token_bytes(nbytes=length)
  9.     key_int = int.from_bytes(key, 'big')
  10.     return key_int
  11. def encrypt(raw):
  12.     raw_bytes = raw.encode()
  13.     raw_int = int.from_bytes(raw_bytes, 'big')
  14.     key_int = random_key(len(raw_bytes))
  15.     return raw_int ^ key_int, key_int
  16. def decrypt(encrypted, key_int):
  17.     decrypted = encrypted ^ key_int
  18.     length = (decrypted.bit_length() + 7) // 8
  19.     decrypted_bytes = int.to_bytes(decrypted, length, 'big')
  20.     return decrypted_bytes.decode()
  21. def encrypt_file(path, key_path=None,):
  22.     document = Document(path)
  23.     all_paragraphs = document.paragraphs
  24.     file = doc.Document()
  25.     file2 = doc.Document()
  26.     jkl = input('请输入希望保存的文件名:') + '.docx'
  27.     for paragraph in all_paragraphs:
  28.         # 打印每一个段落的文字
  29.         zz,key = encrypt(paragraph.text)
  30.         #print('加密:',zz)
  31.         #print('key:', key)
  32.         file.add_paragraph(str(zz))
  33.         file.save(jkl)
  34.         file2.add_paragraph(str(key))
  35.         file2.save("key.docx")
  36. chenggong = encrypt_file(input('输入加密文件名称:'))
  37. print("加密完成!")
  38. time.sleep(10)
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:40 , Processed in 0.019847 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表