吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4290|回复: 6
收起左侧

[Python 转载] 批量加密文件-文件夹加密器(原理:改写文件前20字节)

[复制链接]
a1789043588 发表于 2018-8-19 17:12
萌新发帖,希望大家多多批评指正!
说明:
严重声明!!!萌新第一次写,肯定有bug,用了之后没法还原,概不负责。所以千万不要用于重要文件!
1,不要重复加密,否则覆盖了加密的文件就会导致文件没法复原
2,文件夹不能是纯数字,可能会和编号重复导致BUG。
3,文件总数1000以内# -*- coding: utf-8 -*-
import os
#加密-遍历目录里所有文件,并将其编号,保存文件编号,文件名和文件前20字节到“16.txt”文件里,
#     然后重命名原文件名为其编号
def encryption(dir):
    i=1
    for root, dirs, files in os.walk(dir):
        for file in files:
            fdir=os.path.join(root,file)
            if file !='Thumbs.db':
                with open (fdir,'rb') as f:
                    f.seek(0,0)
                    m=f.read(20)
                    with open ('16.txt','ab+') as ff1:
                            k=ff1.write(str.encode(str(i)) +b'--'+str.encode(file)+b'--'+m+b'\n')
                    with open (fdir,'rb+') as ff2:
                        ff2.seek(0,0)
                        ff2.write(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
                os.rename(fdir,os.path.join(root,str(i)))
                print('加密完',str(i),'文件')
                i+=1
#解密
def decryption(dir):
    ii=1
    for root, dirs, files in os.walk(dir):
        for file in files:
            fdir=os.path.join(root,file)
            if file.isdigit():
                l=0
                m=''
                with open ('16.txt','rb') as fl:
                    lines=fl.readlines()
                ####################################################
                newline = []  
                for i,line in enumerate(lines):
                    if len(line)<20:
                        cur=newline.pop()
                        newline.append(cur+lines)
                    else:
                        newline.append(line)
                lines=newline
                ####################################################
                for line in range(1,int(file)+1):
                    m=lines[line-1]
                    l=l+len(m)
                with open ('16.txt','rb') as f1:
                    f1.seek(l-21,0)
                    k=f1.read(20)
                    f1.seek(l-len(m)+3,0)
                    sname=f1.read(len(m)-26)
                    sname=bytes.decode(sname)
                    if int(file) > 9:
                        sname=sname[1:]
                    if int(file) > 99:
                        sname=sname[1:]
                    if int(file) > 999:
                        sname=sname[1:]
                    with open (fdir,'rb+') as ff1:
                        ff1.seek(0,0)
                        ff1.write(k)
                os.rename(fdir,os.path.join(root,sname))
                print('解密完',str(ii),'文件')
                ii+=1

def 加密(password,dir):
    if password=='不要重复加密,文件夹不能有纯数字的,已备份并清空了备份数据':
        encryption(dir)
    else:
        print('请认真读说明!')
def 解密(dir):
    decryption(dir)

dir="你要加密的文件夹,例如J:\"  
加密('不要重复加密,文件夹不能有纯数字的,已备份并清空了备份数据',dir)
#解密(dir) [/mw_shl_code]

免费评分

参与人数 1吾爱币 +1 收起 理由
silme + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

silme 发表于 2018-8-19 18:23
感觉有点麻烦
wushaominkk 发表于 2018-8-20 09:20
Codeman 发表于 2018-8-20 09:24
zgqc 发表于 2018-8-20 09:51
非常感谢楼主分享
侧写师 发表于 2018-8-21 13:05 来自手机
思路不错 试一把
小黑LLB 发表于 2019-2-11 15:39
有趣 支持一下 楼主 感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-18 02:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表