|
本帖最后由 孙志慧 于 2022-4-8 16:49 编辑
解密过程:
- from secrets import token_bytes
- from docx import Document
- import docx
- import time
- def random_key(length):
- key = token_bytes(nbytes=length)
- key_int = int.from_bytes(key, 'big')
- return key_int
- def encrypt(raw):
- raw_bytes = raw.encode()
- raw_int = int.from_bytes(raw_bytes, 'big')
- key_int = random_key(len(raw_bytes))
- return raw_int ^ key_int, key_int
- def decrypt(encrypted, key_int):
- decrypted = encrypted ^ key_int
- length = (decrypted.bit_length() + 7) // 8
- decrypted_bytes = int.to_bytes(decrypted, length, 'big')
- return decrypted_bytes.decode()
- s = []
- h = []
- def decrypt_file(path_encrypted, key_path=None, *, encoding='utf-8'):
- document = Document(path_encrypted)
- all_paragraphs = document.paragraphs
- do2 = Document('key.docx')
- all_p= do2.paragraphs
- for i in all_paragraphs:
- #str转int
- jiam = int(i.text)
- s.append(jiam)
- for k in all_p:
- key = int(k.text)
- h.append(key)
- c = zip(s,h)
- res = list(c)
- return res
- print('禁止修改密钥文件名')
- rr1 = decrypt_file(input("输入要破解文件名")+'.docx')
- file = docx.Document()
- for i in rr1:
- f = decrypt(*i)
- file.add_paragraph(f)
- file.save("res.docx")
- print('解密完成!')
复制代码
加密过程:- from secrets import token_bytes
- from doc import Document
- import doc
- import time
- def random_key(length):
- # token_bytes,函数接受一个int参数,用于指定随机字节串的长度。
- # int.from_bytes把字节串转换为int,也就是我们需要的二进制数
- key = token_bytes(nbytes=length)
- key_int = int.from_bytes(key, 'big')
- return key_int
- def encrypt(raw):
- raw_bytes = raw.encode()
- raw_int = int.from_bytes(raw_bytes, 'big')
- key_int = random_key(len(raw_bytes))
- return raw_int ^ key_int, key_int
- def decrypt(encrypted, key_int):
- decrypted = encrypted ^ key_int
- length = (decrypted.bit_length() + 7) // 8
- decrypted_bytes = int.to_bytes(decrypted, length, 'big')
- return decrypted_bytes.decode()
- def encrypt_file(path, key_path=None,):
- document = Document(path)
- all_paragraphs = document.paragraphs
- file = doc.Document()
- file2 = doc.Document()
- jkl = input('请输入希望保存的文件名:') + '.docx'
- for paragraph in all_paragraphs:
- # 打印每一个段落的文字
- zz,key = encrypt(paragraph.text)
- #print('加密:',zz)
- #print('key:', key)
- file.add_paragraph(str(zz))
- file.save(jkl)
- file2.add_paragraph(str(key))
- file2.save("key.docx")
- chenggong = encrypt_file(input('输入加密文件名称:'))
- print("加密完成!")
- time.sleep(10)
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|