经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
使用python加密主机文件几种方法实现
来源:jb51  时间:2023/2/10 9:17:49  对本文有异议

数据加密是一种保护数据安全的技术,通过对数据进行编码,使得未经授权的用户无法读取或改动数据。

加密是通过使用加密算法和密钥实现的。加密算法是一种特定的算法,可以将数据编码成不可读的形式,密钥是一串字符串,用于控制加密和解密的过程。加密后的数据只能使用对应的密钥解密。

加密在保护数据的安全性方面非常重要,尤其是在互联网和移动设备环境中,用户的隐私数据容易遭到黑客的攻击。加密技术不仅可以保护数据的隐私,还可以保护数据的完整性,确保数据不被篡改。

在企业环境中,数据加密也被广泛应用于保护公司数据的安全。从合同到秘密文件,加密技术可以保护公司数据不被未经授权的人员查看。

总的来说,数据加密是一种重要的保护数据安全的技术,对于任何用户和企业都非常重要。

要求:利用python对某个目录的全部文件进行加密,密钥由用户的输入的字符串经过变化而生成,封装为一个函数,输入参数有目录路径和用户输入的字符串。

以下是使用 Python 实现的一种简单的文件加密方案:

  1. import os
  2. import hashlib
  3.  
  4. def encrypt_directory(directory, password):
  5. ? ? for root, dirs, files in os.walk(directory):
  6. ? ? ? ? for file in files:
  7. ? ? ? ? ? ? path = os.path.join(root, file)
  8. ? ? ? ? ? ? with open(path, 'rb') as f:
  9. ? ? ? ? ? ? ? ? data = f.read()
  10. ? ? ? ? ? ? key = hashlib.sha256(password.encode()).hexdigest()
  11. ? ? ? ? ? ? encrypted_data = bytearray(x ^ ord(key[i % len(key)]) for i, x in enumerate(data))
  12. ? ? ? ? ? ? with open(path, 'wb') as f:
  13. ? ? ? ? ? ? ? ? f.write(encrypted_data)

该函数接收两个参数:目录路径和用户输入的字符串。它通过使用 os.walk 对目录下的每个文件进行加密,并将加密后的数据写回文件。加密使用了异或操作,密钥是对用户输入的字符串进行 SHA-256 哈希的十六进制字符串。

以下是对上面的加密函数的对应解密函数:

  1. scssCopy code
  2. def decrypt_directory(directory, password):
  3. for root, dirs, files in os.walk(directory):
  4. for file in files:
  5. path = os.path.join(root, file)
  6. with open(path, 'rb') as f:
  7. encrypted_data = f.read()
  8. key = hashlib.sha256(password.encode()).hexdigest()
  9. data = bytearray(x ^ ord(key[i % len(key)]) for i, x in enumerate(encrypted_data))
  10. with open(path, 'wb') as f:
  11. f.write(data)

该函数与上面的加密函数类似,但是它读入加密后的文件,并使用相同的密钥对数据进行异或操作,以获得原始文件。

将上述代码更换加密算法,使用AES加密,代码如下:

  1. import os
  2. import hashlib
  3. from Crypto.Cipher import AES
  4.  
  5. def encrypt_directory(directory, password):
  6. ? ? # 计算密钥
  7. ? ? key = hashlib.sha256(password.encode()).digest()[:16]
  8. ? ? for root, dirs, files in os.walk(directory):
  9. ? ? ? ? for file in files:
  10. ? ? ? ? ? ? path = os.path.join(root, file)
  11. ? ? ? ? ? ? # 读取文件
  12. ? ? ? ? ? ? with open(path, 'rb') as f:
  13. ? ? ? ? ? ? ? ? data = f.read()
  14. ? ? ? ? ? ? # 对数据进行补位
  15. ? ? ? ? ? ? padding_length = 16 - len(data) % 16
  16. ? ? ? ? ? ? data += bytes([padding_length] * padding_length)
  17. ? ? ? ? ? ? # 初始化加密器
  18. ? ? ? ? ? ? cipher = AES.new(key, AES.MODE_ECB)
  19. ? ? ? ? ? ? # 加密数据
  20. ? ? ? ? ? ? encrypted_data = cipher.encrypt(data)
  21. ? ? ? ? ? ? # 将加密后的数据写回文件
  22. ? ? ? ? ? ? with open(path, 'wb') as f:
  23. ? ? ? ? ? ? ? ? f.write(encrypted_data)
  24.  
  25. def decrypt_directory(directory, password):
  26. ? ? # 计算密钥
  27. ? ? key = hashlib.sha256(password.encode()).digest()[:16]
  28. ? ? for root, dirs, files in os.walk(directory):
  29. ? ? ? ? for file in files:
  30. ? ? ? ? ? ? path = os.path.join(root, file)
  31. ? ? ? ? ? ? # 读取文件
  32. ? ? ? ? ? ? with open(path, 'rb') as f:
  33. ? ? ? ? ? ? ? ? encrypted_data = f.read()
  34. ? ? ? ? ? ? # 初始化解密器
  35. ? ? ? ? ? ? cipher = AES.new(key, AES.MODE_ECB)
  36. ? ? ? ? ? ? # 解密数据
  37. ? ? ? ? ? ? data = cipher.decrypt(encrypted_data)
  38. ? ? ? ? ? ? # 删除补位数据
  39. ? ? ? ? ? ? padding_length = data[-1]
  40. ? ? ? ? ? ? data = data[:-padding_length]
  41. ? ? ? ? ? ? # 将解密后的数据写回文件
  42. ? ? ? ? ? ? with open(path, 'wb') as f:
  43. ? ? ? ? ? ? ? ? f.write(data)

注:上面的代码仅供参考,不建议在生产环境中使用。AES ECB 模式并不是很安全,应该使用其他模式。

或者使用非对称加密:

这里使用RSA加密算法实现数据的加密解密:

  1. import os
  2. import rsa
  3.  
  4. def encrypt_file(file_path, public_key_file):
  5. ? ? """使用RSA算法加密文件
  6. ? ??
  7. ? ? 参数:
  8. ? ? file_path: 需要加密的文件路径
  9. ? ? public_key_file: 公钥文件路径
  10. ? ??
  11. ? ? 返回值:
  12. ? ? 无
  13. ? ? """
  14. ? ? # 读取文件内容
  15. ? ? with open(file_path, "rb") as file:
  16. ? ? ? ? file_content = file.read()
  17. ? ? # 读取公钥
  18. ? ? with open(public_key_file, "rb") as key_file:
  19. ? ? ? ? public_key = rsa.PublicKey.load_pkcs1(key_file.read())
  20. ? ? # 加密文件内容
  21. ? ? encrypted_content = rsa.encrypt(file_content, public_key)
  22. ? ? # 将加密后的内容写入文件
  23. ? ? with open(file_path, "wb") as file:
  24. ? ? ? ? file.write(encrypted_content)
  25.  
  26. def decrypt_file(file_path, private_key_file, password):
  27. ? ? """使用RSA算法解密文件
  28. ? ??
  29. ? ? 参数:
  30. ? ? file_path: 需要解密的文件路径
  31. ? ? private_key_file: 私钥文件路径
  32. ? ? password: 私钥文件密码
  33. ? ??
  34. ? ? 返回值:
  35. ? ? 无
  36. ? ? """
  37. ? ? # 读取文件内容
  38. ? ? with open(file_path, "rb") as file:
  39. ? ? ? ? encrypted_content = file.read()
  40. ? ? # 读取私钥
  41. ? ? with open(private_key_file, "rb") as key_file:
  42. ? ? ? ? private_key = rsa.PrivateKey.load_pkcs1(key_file.read(), password)
  43. ? ? # 解密文件内容
  44. ? ? file_content = rsa.decrypt(encrypted_content, private_key)
  45. ? ? # 将解密后的内容写入文件
  46. ? ? with open(file_path, "wb") as file:
  47. ? ? ? ? file.write(file_content)

需要注意的是,RSA加密的效率较低,适用于加密少量数据,如对文件进行加密

到此这篇关于使用python加密主机文件几种方法实现的文章就介绍到这了,更多相关python加密主机文件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号