logo图片
Python密码学

文件加密

文件加密详细操作教程
在Python中,可以在传输到通信通道之前对文件进行加密和解密。为此,您将必须使用插件 PyCrypto 。您可以使用下面给出的命令安装此插件。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
pip install pycrypto
 PyCrypto

代码

下面提到用密码保护器加密文件的程序代码-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
# =================Other Configuration================
# Usages :
usage = "usage: %prog [options] "
# Version
Version="%prog 0.0.1"
# ====================================================
# import Modules
import optparse, sys,os
from toolkit import processor as ps
def main():
   parser = optparse.OptionParser(usage = usage,version = Version)
   parser.add_option(
      '-i','--input',type = 'string',dest = 'inputfile',
      help = "File Input Path for Encryption", default = None)
   parser.add_option(
      '-o','--output',type = "string",dest = 'outputfile',
      help = "File Output Path for Saving Encrypter Cipher",default = ".")

   parser.add_option(
      '-p','--password',type = "string",dest = 'password',
      help = "Provide Password for Encrypting File",default = None)

   parser.add_option(
      '-p','--password',type = "string",dest = 'password',
      help = "Provide Password for Encrypting File",default = None)

   (options, args)= parser.parse_args()

   # Input Conditions Checkings
   if not options.inputfile or not os.path.isfile(options.inputfile):
      print " [Error] Please Specify Input File Path"
      exit(0)
   if not options.outputfile or not os.path.isdir(options.outputfile):
      print " [Error] Please Specify Output Path"
      exit(0)
   if not options.password:
      print " [Error] No Password Input"
      exit(0)
   inputfile = options.inputfile
   outputfile = os.path.join(
      options.outputfile,os.path.basename(options.inputfile).split('.')[0]+'.ssb')
   password = options.password
   base = os.path.basename(inputfile).split('.')[1]
   work = "E"
   ps.FileCipher(inputfile,outputfile,password,work)
   return
   if __name__ == '__main__':
   main()
您可以使用以下命令与密码一起执行加密过程-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
python pyfilecipher-encrypt.py -i file_path_for_encryption -o output_path -p password

输出

执行上面给出的代码时,您可以观察到以下输出-
加密过程

说明

使用MD5哈希算法生成密码,并将值存储在Windows系统中的简单安全备份文件中,其中包括如下所示的值-
说明
Python 拓展教程
Copyright © 2020 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4