使用python加密主机文件几种方法实现_程序员王炸
今天其主要简单推荐了使用的python图片加密主机结构zip文件多少种的办法实行,这篇文章按照样例二维码简单推荐的尤其全面,对许多人的借鉴一些操作享有一段的参考使用借鉴使用价值,要的好友 们后面伴随编辑来混着借鉴借鉴吧
12345678910111213 | importos importhashlib defencrypt_directory(directory, password): forroot, dirs, files inos.walk(directory): forfileinfiles: path =os.path.join(root, file) with open(path, 'rb') as f: data =f.read() key =hashlib.sha256(password.encode()).hexdigest() encrypted_data =bytearray(x ^ ord(key[i %len(key)]) fori, x inenumerate(data)) with open(path, 'wb') as f: f.write(encrypted_data) |
1234567891011 | scssCopy code defdecrypt_directory(directory, password): forroot, dirs, files inos.walk(directory): forfileinfiles: path =os.path.join(root, file) with open(path, 'rb') as f: encrypted_data =f.read() key =hashlib.sha256(password.encode()).hexdigest() data =bytearray(x ^ ord(key[i %len(key)]) fori, x inenumerate(encrypted_data)) with open(path, 'wb') as f: f.write(data) |
12345678910111213141516171819202122232425262728293031323334353637383940414243 | importos importhashlib fromCrypto.Cipher importAES defencrypt_directory(directory, password): # 计算密钥 key =hashlib.sha256(password.encode()).digest()[:16] forroot, dirs, files inos.walk(directory): forfileinfiles: path =os.path.join(root, file) # 读取文件 with open(path, 'rb') as f: data =f.read() # 对数据进行补位 padding_length =16-len(data) %16 data +=bytes([padding_length] *padding_length) # 初始化加密器 cipher =AES.new(key, AES.MODE_ECB) # 加密数据 encrypted_data =cipher.encrypt(data) # 将加密后的数据写回文件 with open(path, 'wb') as f: f.write(encrypted_data) defdecrypt_directory(directory, password): # 计算密钥 key =hashlib.sha256(password.encode()).digest()[:16] forroot, dirs, files inos.walk(directory): forfileinfiles: path =os.path.join(root, file) # 读取文件 with open(path, 'rb') as f: encrypted_data =f.read() # 初始化解密器 cipher =AES.new(key, AES.MODE_ECB) # 解密数据 data =cipher.decrypt(encrypted_data) # 删除补位数据 padding_length =data[-1] data =data[:-padding_length] # 将解密后的数据写回文件 with open(path, 'wb') as f: f.write(data) |
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | importos importrsa defencrypt_file(file_path, public_key_file): """使用RSA算法加密文件
参数: file_path: 需要加密的文件路径 public_key_file: 公钥文件路径
返回值: 无 """ # 读取文件内容 with open(file_path, "rb") as file: file_content =file.read() # 读取公钥 with open(public_key_file, "rb") as key_file: public_key =rsa.PublicKey.load_pkcs1(key_file.read()) # 加密文件内容 encrypted_content =rsa.encrypt(file_content, public_key) # 将加密后的内容写入文件 with open(file_path, "wb") as file: file.write(encrypted_content) defdecrypt_file(file_path, private_key_file, password): """使用RSA算法解密文件
参数: file_path: 需要解密的文件路径 private_key_file: 私钥文件路径 password: 私钥文件密码
返回值: 无 """ # 读取文件内容 with open(file_path, "rb") as file: encrypted_content =file.read() # 读取私钥 with open(private_key_file, "rb") as key_file: private_key =rsa.PrivateKey.load_pkcs1(key_file.read(), password) # 解密文件内容 file_content =rsa.decrypt(encrypted_content, private_key) # 将解密后的内容写入文件 with open(file_path, "wb") as file: file.write(file_content) |
300+皇冠新体育APP经典编程案例
50G+学习视频教程
点击拿去
皇冠新体育APP相关的文章
- 皇冠新体育APP:数据结构(C语言第2版) 课后习题答案之 第三章 栈和队列_哆啦A梦_i
- 皇冠新体育APP:IO流、File类、节点流、缓冲流、转换流_托比-马奎尔
- 皇冠新体育APP:自动化测试----selenium(一)_半夏而凉_selenium自动化测试
- 两台win10电脑搭建scrapy_redis实现分布式爬虫_GIS从业者
- 皇冠新体育APP:企业级分布式爬虫框架入门_真?skysys_企业级爬虫项目
- 实战使用scrapy与selenium来爬取数据_尽君欢
- 一份详实的 Scrapy 爬虫教程,从原理到实战_npm_run_dev__
- gerapy运行scrapy程序,报错timeout_匿名者_Anonym
- SQL Server的timestamp映射到Java的java.sql.Timestamp出现的问题_abckingaa
- 关于错误:[Error] expected primary-expression before ‘int‘_qq_52634210
- 皇冠新体育APP:杨辉三角(C语言实现)_十七ing_杨辉三角的规律c语言
- 自动化测试-单元测试_全栈开发与测试_自动化单元测试
- 皇冠新体育APP:Scrapy框架介绍_冷巷(?_?)_scrapy框架介绍
- 皇冠新体育APP:Scrapy + selenium + 超级鹰验证码识别爬取网站_李甜甜~
- 皇冠新体育APP:scrapy简单实现一个项目_<编程路上>
- 皇冠新体育APP:python单元测试框架Unittest详解_测试框架师?九_python unittest框架