Python encrypts word documents

  Hello, everyone, this is the funny Institute. In this issue, we need to complete a program to encrypt word documents. After reading some materials and articles on the Internet, it is considered that using XOR to encrypt word documents is more convenient and secure.
  before that, let's learn what XOR is. Simply put, if the values of a and b are different, the XOR result is 1. If the values of a and b are the same, the XOR result is 0. Let's simply sort out the code ideas. The code is divided into two parts, encryption and decryption.
  1. encryption
  convert the file into binary format, and then generate a random key of equal length for XOR operation to obtain the encrypted binary file. The data we need to keep in this step are encrypted files and randomly generated keys. Of course, they are all binary numbers.
  2. decrypt
   this step is simple. We perform an XOR operation on the encrypted file and the previously randomly generated key to obtain the original binary number, and then we convert it into text.
OK, the idea is roughly clear. We need two programs. The encryption program receives str parameters and will output the encrypted binary word document and the binary key for decryption. The decryption program needs to receive two int parameters, which are the two binary contents output by the encryption program, and output the original text after XOR. So, the code.
Encryption code:

from secrets import token_bytes
from docx import Document
import docx
import time
​
def random_key(length):
    # token_bytes, the function accepts an int parameter that specifies the length of a random byte string.
    # int.from_bytes converts the byte string to int, that is, the binary number we need
    key = token_bytes(nbytes=length)
    key_int = int.from_bytes(key, 'big')
    return key_int
​
def encrypt(raw):
    raw_bytes = raw.encode()
    #The parameter big means positive order, and little means reverse order.
    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 = docx.Document()
    file2 = docx.Document()
​
    jkl = input('Please enter the file name you want to save:') + '.docx'
​
    for paragraph in all_paragraphs:
        # Print text for each paragraph
        zz,key = encrypt(paragraph.text)
​
        #print('encryption: ', zz)
        #print('key:', key)
​
        file.add_paragraph(str(zz))
        file.save(jkl)
​
        file2.add_paragraph(str(key))
        file2.save("key.docx")
​
print('Funny Institute!')
print('Only English file names are supported.')
chenggong = encrypt_file(input('Please enter the file name to be encrypted:'))
print("Completed! Automatic shutdown after ten seconds")
time.sleep(10)
#Generate encrypted file

   encode the string into a byte string through the encode method. int.from_ The bytes function converts a byte string to an int object. Finally, the encrypted text is obtained by XOR operation between binary object and random key.
Decryption code:

from secrets import token_bytes
from docx import Document
import docx
import time
​
def random_key(length):
    # token_bytes, the function accepts an int parameter that specifies the length of a random byte string.
    # int.from_bytes converts the byte string to int, that is, the binary number we need
    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()
​
jjj = []
kkk = []
​
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 to int
        jiam = int(i.text)
        jjj.append(jiam)
​
        #print('encryption: ', jiam)
    #print(jjj)
​
    for k in all_p:
        #str to int
        key = int(k.text)
        kkk.append(key)
​
        #print('key:',key)
    #print(kkk)
​
    cc = zip(jjj,kkk)
    res = list(cc)
    return res
#Pass in tuples, or two int s.
print('Funny Institute!')
print('Warning, do not modify the key file name!!!')
print('Directly enter the file name without format suffix.')
rr1 = decrypt_file(input("Please enter the file name of the file to be cracked(Only.docx file): ")+'.docx')
​
file = docx.Document()
for i in rr1:
    ff = decrypt(*i)
    #print(ff)
    #print(type(ff))
    file.add_paragraph(ff)
file.save("res.docx")
print('Decryption is complete, please extract the file in the current folder!')
print('Turn it off automatically in ten seconds!')
time.sleep(10)

  we need to package the two programs into exe. The binary word document obtained by running the encryption program can be given to others, but the key must be saved by itself. When others meet your requirements, we can give them the key and decryption program. Note that it is only valid for docx files, and the name of the key file cannot be modified, otherwise an error will be reported and decryption will fail.
  operation results:
  original word file.

  after encryption:

  generated key key:

   after the encrypted file and the generated key are placed in the decryption program folder, the following results will be obtained. We got the original file. The downside is that all the first line indents have disappeared and become left aligned.

  after re encrypting the same file, you will get different encrypted files and keys. Therefore, if the encrypted file and the key do not match, even if their source files are the same, they cannot be decrypted. In addition, if the key is lost, the encrypted file will never be decrypted. Oh, my God. If someone can try out random keys of the same length one by one. If monkeys can write Notre Dame de Paris.
  if you are interested in XOR text encryption, you can take a look at this article and thank the author for providing ideas.

https://blog.csdn.net/xiaofeixia666888/article/details/102613213

Keywords: Python

Added by simon13 on Sat, 25 Dec 2021 03:30:16 +0200