python对某些文件加密(解密)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:


提示:以下是本篇文章正文内容,下面案例可供参考

一、函数加密?

示例:

二、使用步骤

1.引入库

代码如下(示例):


pip install cryptography

2.函数

代码如下(示例):



import base64
from cryptography.fernet import Fernet
import argon2


class XDEncrypt():
    def __init__(self):
        pass

    def encrypt(self, password, in_file, out_file):
        argon2_parameters = argon2.Parameters(type=argon2.low_level.Type.ID, version=19, salt_len=16, hash_len=32, time_cost=16,
                          memory_cost=2 ** 20, parallelism=8)
        hasher = argon2.PasswordHasher(time_cost=argon2_parameters.time_cost, memory_cost=argon2_parameters.memory_cost,
                                       parallelism=argon2_parameters.parallelism, hash_len=argon2_parameters.hash_len,
                                       salt_len=argon2_parameters.salt_len)
        pw_hash = hasher.hash(password)
        salt = pw_hash.split("$")[-2]
        raw_hash = argon2.low_level.hash_secret_raw(time_cost=argon2_parameters.time_cost,
                                                    memory_cost=argon2_parameters.memory_cost,
                                                    parallelism=argon2_parameters.parallelism,
                                                    hash_len=argon2_parameters.hash_len,
                                                    secret=bytes(password, "utf_16_le"), salt=bytes(salt, "utf_16_le"),
                                                    type=argon2_parameters.type)
        key = base64.urlsafe_b64encode(raw_hash)
        fernet = Fernet(key)
        with open(in_file, 'rb') as f_in,open(out_file, 'wb+') as f_out:
                data = f_in.read()
                enc_data = fernet.encrypt(data)
                pw_hash = pw_hash + '\n'
                f_out.write(bytes(pw_hash, "utf-8"))
                f_out.write(enc_data)
                print('加密完成')

    def decrypt(self, password, in_file):
        with open(in_file, 'r') as f:
            pw_hash = f.readline()[:-1]
        p = argon2.extract_parameters(pw_hash)
        hasher = argon2.PasswordHasher(time_cost=p.time_cost,
                                       memory_cost=p.memory_cost,
                                       parallelism=p.parallelism,
                                       hash_len=p.hash_len,
                                       salt_len=p.salt_len)
        try:
            hasher.verify(pw_hash, password)
            print("密码校验成功")
        except:
            print("密码校验失败")
            exit()
        salt = pw_hash.split("$")[-2]
        raw_hash = argon2.low_level.hash_secret_raw(time_cost=p.time_cost,
                                                    memory_cost=p.memory_cost,
                                                    parallelism=p.parallelism,
                                                    hash_len=p.hash_len,
                                                    secret=bytes(password, "utf_16_le"),
                                                    salt=bytes(salt, "utf_16_le"),
                                                    type=p.type)
        key = base64.urlsafe_b64encode(raw_hash)
        fernet = Fernet(key)
        dec_data = b''
        with open(in_file, 'rb') as f_in:
            enc_data = f_in.readlines()[1]
            try:
                dec_data = fernet.decrypt(enc_data)
                print("decryption succeed")
            except:
                print("decryption failed")
        return dec_data


if __name__ == '__main__':
    xde = XDEncrypt()
    # xde.encrypt(password='123456', in_file=r'D:\xunlian\utils\doc\df.py', out_file=r'D:\xunlian\utils\doc\df_b.py')
    res = xde.decrypt(password='123456', in_file=r'D:\xunlian\utils\doc\df_b.py')
    print(res)
    pass

---

# 总结
提示:这里对文章进行总结:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值