用云盘和移动硬盘数据安全性都知道的,坚果云之前直接不建议用来同步代码,于是做个自动加解密的工具:
(可以将解密地址设计成桌面,实现拖动解密,第一次用而且数据量较大,于是加了个哈希验证,后面可以去掉)
缺点,暂时对大文件支持不是很友好,占用内存,应该有类似哈希的update方法。
if __name__ == "__main__": while True: print('开始%s'%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) # 自动加密阶段 encpath = r'C:\Users\Administrator\Desktop\加密' files = os.walk(encpath) for path,dirlist,filelist in files: for file in filelist: if'baiduyun.uploading.cfg' in file: continue if not file.startswith('enc777'): file_path = os.path.join(path, file) with open(file=os.path.join(path,file),mode='rb') as f: file_content = f.read() content_hash = hashlib.md5(file_content).hexdigest() aes = AESCipher() enc_content = aes.encrypt(file_content) dec_contnet = aes.decrypt(enc_content) content_hashdec = hashlib.md5(dec_contnet).hexdigest() if content_hashdec==content_hash: with open(os.path.join(path,'enc777'+file),'wb+') as f: f.write(enc_content) os.remove(os.path.join(path,file)) else: print('前后不一样拒绝加密') continue # 自动解密阶段: decpath = r'C:\Users\Administrator\Desktop\解密' files = os.walk(decpath) for path, dirlist, filelist in files: for file in filelist: if file.startswith('enc777'): file_path = os.path.join(path, file) with open(file=os.path.join(path, file), mode='rb') as f: file_content = f.read() content_hash = hashlib.md5(file_content).hexdigest() aes = AESCipher() dec_content = aes.decrypt(file_content) enc_contnet = aes.encrypt(dec_content) content_hashdec = hashlib.md5(enc_contnet).hexdigest() # if content_hashdec == content_hash: if True: with open(os.path.join(path, file[6:]), 'wb+') as f: f.write(dec_content) os.remove(os.path.join(path, file)) else: print('前后不一样拒绝解密') # 再加密时iv不同,因此凉凉 continue time.sleep(1) # print('加解密完成')