Nejlepší odpověď
Existuje mnoho možností k šifrování souboru v Pythonu.
Používání knihovny PyCrypto: PyCrypto je kolekce zabezpečených hash funkcí a různých šifrovacích algoritmů. Nejprve nainstalujte PyCrypto balíček spuštěním tohoto příkazu na CMD
 pip install pycrypto 
Kód pro šifrování souborů pomocí algoritmu AES je uveden níže
 import os, random, struct 
 from Crypto.Cipher import AES 
 def encrypt\_file(key, in\_filename, out\_filename=None, chunksize=64*1024): 
  """ Encrypts a file using AES (CBC mode) with the 
  given key. 
  key: 
  The encryption key - a string that must be 
  either 16, 24 or 32 bytes long. Longer keys 
  are more secure. 
  in\_filename: 
  Name of the input file 
  out\_filename: 
  If None, " 
  chunksize: 
  uses to read and encrypt the file. Larger chunk 
  sizes can be faster for some files and machines. 
  chunksize must be divisible by 16. 
  """ 
  if not out\_filename: 
  out\_filename = in\_filename + ".enc" 
  iv = "".join(chr(random.randint(0, 0xFF)) for i in range(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(" 
  outfile.write(iv) 
  while True: 
  chunk = infile.read(chunksize) 
  if len(chunk) == 0: 
  break 
  elif len(chunk) \% 16 != 0: 
  chunk += " " * (16 - len(chunk) \% 16) 
  outfile.write(encryptor.encrypt(chunk)) 
Používání PyAesCrypt: Modul šifrování tří souborů Pythonu, který k šifrování a dešifrování souborů používá AES256-CBC
 pip install pyAesCrypt 
Ukázkový kód zobrazený níže:
 import pyAesCrypt 
 # encryption/decryption buffer size - 64K 
 bufferSize = 64 * 1024 
 password = "foopassword" 
 # encrypt 
 pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize) 
 # decrypt 
 pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize) 
Dobrou volbou pro šifrování může být také knihovna pro kryptografii v Pythonu
Odpověď
  
Nejjednodušší způsob šifrování souborů pomocí pythonu bude použití modul kryptografie .
1. Instalace:
 pip/pip3 install cryptography 
2. Kód:
Budeme mít videofilm s názvem „ myvideo.mp4 “ k zašifrování a hlavního programu “main.py” .
 """ 
 filename: main.py 
 author: SANDIPAN CHOWDHURY 
 purpose: Encrypting any file 
 This program is posted on Quora on 9th January, 2021 
 """ 
 #imports 
 from cryptography.fernet import Fernet 
 def sancrypt(filename): 
  "This function Encrypt the file" 
  key=Fernet.generate\_key() 
  key\_master=Fernet(key) 
  with open (filename,"rb") as f: 
  data=f.read() 
  encr\_data=key\_master.encrypt(data) 
  with open(filename+".sancrypted","wb") as f: 
  f.write(encr\_data) 
  with open(filename+".key","wb") as f: 
  f.write(key) 
 def desancrypt(filename,keyfilename): 
  "This function decrypt the filename with keyfilename" 
  with open(filename,"rb") as f: 
  encr\_data=f.read() 
  with open(keyfilename,"rb") as f: 
  key=f.read() 
  keymaster=Fernet(key) 
  decr\_data=keymaster.decrypt(encr\_data) 
  with open(filename.replace(".sancrypted",""),"wb") as f: 
 def main(): 
  x=input(""" 
 1. Encrypt 
 2. Decrypt 
 Enter a number? 
 """) 
  if x=="1": 
  filename=input("Enter a filename: ") 
  try: 
  sancrypt(filename) 
  print("File Encrypted.") 
  main() 
  except Exception as e: 
  print("File Encryption Failed") 
  print(str(e)) 
  main() 
  elif x=="2": 
  filename=input("Enter filename with ".sancrypted" extension: ") 
  keyfile=input("Enter filename with ".key" extension: ") 
  try: 
  desancrypt(filename,keyfile) 
  print("File Decrypted.") 
  main() 
  except Exception as e: 
  print("Decryption Failed.") 
  print(str(e)) 
  main() 
  else: 
  print("Invalid Input. Must be 1 or 2") 
  main() 
 if \_\_name\_\_=="\_\_main\_\_": 
  main() 
Vždy se snažte použít správný soubor klíčů datový soubor, buď určitě selže. Psaní tohoto programu trvá asi 30 minut. Hlasujte pro moji odpověď.