-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
147 lines (132 loc) · 4.65 KB
/
file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#-*- coding:utf-8 -*-
#######pyqt 文件载入对话框,文件保存对话框,打开文件夹对话框
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import tarfile
import gzip, os
from M2Crypto.EVP import Cipher
from M2Crypto import m2
from M2Crypto import util
ENCRYPT_OP = 1
DECRYPT_OP = 0
iv = '\0' * 16 # init not used for aes_128_ecb
PRIVATE_KEY = "hsylgwk-2012aaaa"
class MyWindow(QDialog,QWidget):
def __init__(self,parent = None):
super(MyWindow,self).__init__(parent)
self.resize(400,150)
self.mainlayout = QGridLayout(self)
"""
self.loadFileButton = QPushButton()
self.loadFileButton.setText(u"载入文件")
self.mainlayout.addWidget(self.loadFileButton,0,0,1,1)
self.loadFileLineEdit = QLineEdit()
self.mainlayout.addWidget(self.loadFileLineEdit,0,1,1,4)
self.loadFileButton.clicked.connect(self.loadFile)
self.saveFileButton = QPushButton()
self.saveFileButton.setText(u"保存文件")
self.saveFileLineEdit = QLineEdit()
self.mainlayout.addWidget(self.saveFileButton,1,0,1,1)
self.mainlayout.addWidget(self.saveFileLineEdit,1,1,1,4)
self.saveFileButton.clicked.connect(self.saveFile)
self.openFileDirButton = QPushButton()
self.openFileDirButton.setText(u"打开文件目录")
self.mainlayout.addWidget(self.openFileDirButton,2,0,1,1)
self.openFileDirButton.clicked.connect(self.openFileDirectory)
"""
self.openFileDirButton = QPushButton()
self.openFileDirButton.setText(u"打包压缩并加密文件夹")
self.mainlayout.addWidget(self.openFileDirButton, 3, 0, 1, 1)
self.openFileDirButton.clicked.connect(self.compress)
"""
def loadFile(self):########载入file
file_name = QFileDialog.getOpenFileName(self,"open file dialog","C:\Users\Administrator\Desktop","Txt files(*.txt)")
##"open file Dialog "文件对话框的标题,第二个是打开的默认路径,第三个是文件类型
self.loadFileLineEdit.setText(file_name)
def saveFile(self):
self.file_path = QFileDialog.getSaveFileName(self,'save file',"saveFile" ,"xj3dp files (*.xj3dp);;all files(*.*)") ####
print self.file_path
def openFileDirectory(self):
import os
os.popen("explorer.exe C:\Users\Administrator\Desktop")
"""
def compress(self):
path = QFileDialog.getExistingDirectory()
print path
#path +='/'
tar_file_gzip(str(path))
sys.exit()
def Encrypt(data):
print "Enc len=",len(data)
cipher = Cipher(alg='aes_128_ecb', key=PRIVATE_KEY, iv=iv, op=ENCRYPT_OP)
buf = cipher.update(data)
buf = buf + cipher.final()
del cipher
return buf
"""
output = ''
for i in buf:
output += '%02X' % (ord(i))
return output"""
def Decrypt(data):
#data = util.h2b(data)
print "Dec len=", len(data)
cipher = Cipher(alg='aes_128_ecb', key=PRIVATE_KEY, iv=iv, op=DECRYPT_OP)
buf = cipher.update(data)
buf = buf + cipher.final()
del cipher
return buf
def tar_file_gzip(path):
tar = tarfile.open("ttar.tar.gz", "w:gz")
try:
root, dir, files = os.walk(path)
except Exception, e:
print e
for root, dir, files in os.walk(path):
for file in files:
fullpath = os.path.join(root, file)
tar.add(fullpath)
tar.close()
aes_file("ttar.tar.gz")
def aes_file(filename):
fd = open(filename, 'rb')
fistat = os.stat(filename)
lenthWr = 0
fdout = open("update.bin", 'wb')
fdD = open("update.tar.gz", 'wb')
wrlen = 0
"""
while fistat.st_size - lenthWr > 127:
datai = fd.read(127)
print "read len=", len(datai)
datao = Encrypt(datai)
print "after enc len=",len(datao)
fdout.write(datao)
decry = Decrypt(datao)
print "after dec len=", len(decry)
wrlen += len(decry)
fdD.write(decry)
lenthWr += 127;
print lenthWr
print "write = ",wrlen"""
while fistat.st_size - lenthWr > 0:
datai = fd.read(15)
print "read len=", len(datai)
datao = Encrypt(datai)
fdout.write(datao)
decry = Decrypt(datao)
print "after dec len=", len(decry)
wrlen += len(decry)
fdD.write(decry)
lenthWr += 15
print lenthWr
print "write = ", wrlen
fd.close()
fdD.close()
fd.close()
app=QApplication(sys.argv)
window=MyWindow()
window.show()
app.exec_()
print app