
This is of course a famous poem by Cao Cao,我们使用AESalgorithm to encrypt and decrypt.AESIs a very mature、安全、Internationally used encryption and decryption algorithm for symmetric ciphers,供AESAn important parameter for encryption and decryption is the key.This key is just a random string,通常是128位或256位字长.AESThe key used for encryption and decryption is no different from other cryptographic algorithms.废话不说,直接上代码.
import os import struct from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad defaultsize = 64*1024 def encrypt_file(key, in_filename, out_filename=None, chunksize=defaultsize): """ 对文件进行加密 Args: key (str): 16字节密钥 in_filename (str): 待加密文件 out_filename (str, optional): The encrypted output file chunksize (int, optional): 块大小,缺省64k """ if not out_filename: out_filename = in_filename + '.enc' iv = os.urandom(16) encryptor = AES.new(key, AES.MODE_CBC, iv) filesize = os.path.getsize(in_filename) with open(in_filename, 'rb') as infile: with open(out_filename, 'wb') as outfile: outfile.write(struct.pack('<Q', filesize)) outfile.write(iv) pos = 0 while pos < filesize: chunk = infile.read(chunksize) pos += len(chunk) if pos == filesize: chunk = pad(chunk, AES.block_size) outfile.write(encryptor.encrypt(chunk)) def decrypt_file(key, in_filename, out_filename=None, chunksize=defaultsize): """ 解密文件 Args: key (str): 16字节密钥 in_filename (str): 待解密文件 out_filename (str, optional): The decrypted output file chunksize (int, optional): 块大小,缺省64K """ if not out_filename: out_filename = in_filename + '.dec' with open(in_filename, 'rb') as infile: filesize = struct.unpack('<Q', infile.read(8))[0] iv = infile.read(16) encryptor = AES.new(key, AES.MODE_CBC, iv) with open(out_filename, 'wb') as outfile: encrypted_filesize = os.path.getsize(in_filename) pos = 8 + 16 # the filesize and IV. while pos < encrypted_filesize: chunk = infile.read(chunksize) pos += len(chunk) chunk = encryptor.decrypt(chunk) if pos == encrypted_filesize: chunk = unpad(chunk, AES.block_size) outfile.write(chunk) # 密钥,随便写,Use only before use16字节 key = 'stayhungrystayfoolish' def test_enc(): sourcefilename=r'../resources/神龟虽寿.docx' encrypt_file(key[:16].encode('utf-8'), sourcefilename) def test_dec(): sourcefilename=r'../resources/神龟虽寿.docx.enc' targetfilename=r'../resources/decrypt.docx' decrypt_file(key[:16].encode('utf-8'), sourcefilename,out_filename=targetfilename) if __name__ == '__main__': test_enc() test_dec()
我们这里使用128Bit key for encryption and decryption,The encrypted file is Microsoft'sWORD也打不开,The internal structure has been completely changed,The decrypted file is no different from the original document.从代码可以看出,The encryption and decryption of files provided in this article has nothing to do with the file type,It is completely possible to use this algorithm for music、Encrypt and decrypt video, etc,If used togetherQT等开发工具,Can be further upgraded to be easy to useGUIInterface file encryption and decryption.补充一点,The above encryption and decryption algorithm library needs to install the corresponding package,使用以下命令即可.
pip install PyCryptodome