模块:
from optparse import OptionParser
from optparse import OptionGroup
**#解决中文帮助乱码
reload(sys)
sys.setdefaultencoding('utf-8')**
USAGE='usage: %prog [option] args \n默认工作模式为ECB'
parser=OptionParser(USAGE)
#文件操作选项(分组)
group1=OptionGroup(parser,"文件操作")
group1.add_option('--keyfile',action='store',dest='keyfile',default=False,help='密钥文件')
#基本选项
parser.add_option('-m','--mode',action='store',type='int',dest='mode',default=False,help='选择加密为1 选择解密为2')
#添加分组选项
parser.add_option_group(group1)
#参数处理
(opts,args)=parser.parse_args(sys.argv)
#操作参数
#是否设置为自测
if opts.test!=False:
......
#调用帮助文档
def doHelp():
global parser
parser.print_help()
sys.exit(1)
def goodkey(key):
if isinstance(key, basestring):
mylen=len(key)
else:
mylen=key.size()
#print mylen
if mylen<8:
key=key.ljust(8-len(key),'\x00')
elif mylen>8:
key=key
return key
**import threading
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name=name
self.func=func
self.args=args
def run(self):
#print 'starting',self.name,'at:',ctime()
self.res=apply(self.func,self.args)
#print self.name,'finished at:',ctime()
def getResult(self):
return self.res**
for i in nloops:
t=MyThread(self.workThread,(nblock,thblock[i]),self.workThread.__name__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
#MyGlobal.resTHREAD.append(threads[i].getResult())
resHex_th=self.resultHex(threads[i].getResult(),i)
#将每个线程返回的结果追加到文件中
self.saveResult(resHex_th)
import mmap
def rfMmap(self,path):
#内存映射
if os.path.isfile(path):
filename=path
else:
print "没有 %s 这个文件" % str(path)
sys.exit(2)
fobj=file(filename,"rb+")
self.size=os.path.getsize(filename)
fmap=mmap.mmap(fobj.fileno(),0)
return fmap
**>>> import binascii
binascii.b2a_hex("abc")**
**#字符串转换16进制字符串
'616263'
binascii.a2b_hex("616263")**
**#十六进制字符串转换字符串
'abc'
ord("a")**
**#asc码数值
97
chr(97)
'a'
def h2b_bin(self,arghex):**
#十六进制字符串转换2进制字符串
return ''.join(MyGlobal.hex_bin_map[x] for x in arghex)
def b2h_hex(self,argbin):
#二进制字符串转换十六进制字符串
return ''.join(hex(int(argbin,2))[2:])
def int2bytes(self,argint):
#将数值转换成字节,填充补足的时候使用
return MyGlobal.int_bytes_map
def oxrStr(self,a,b):
data={}
data['0','1']=data['1','0']='1'
data['0','0']=data['1','1']='0'
s=[data[a[x],b[x]] for x in range(a.__len__())]
return ''.join(s)
return c
def sBox(self,bpart):
** res=""
for i in range(8):
row=int(bpart[i*6]+bpart[5+6*i],2)
col=int(bpart[1+i*6:5+i*6],2)
tmp=bin(MyGlobal.SBOX[i][row][col])[2:]**
res+=tmp.rjust(4-len(tmp),'0') #将二进制字符串以0填充至4位
return res5.按要求
def permutate(self,table,block):
#按表置换
return map(lambda x:block[x-1],table)
def loopMove(self,cd_key,i):
#按表循环左移
step=MyGlobal.MoveBit
return cd_key[step:]+cd_key[:step]
if MyGlobal.MODE==2:
#解密时将子密钥逆转
MyGlobal.SUBKEY.reverse()
import math
#import pdb
#pdb.set_trace()
**import sys,os
mypath=os.path.abspath('../comm_des')
if mypath not in sys.path:
sys.path.append(mypath)
from mylib import *
from MyGlobal import *
from commline import *
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
**
class des_gui:
def __init__(self):
self.gladefile="des_gui.glade"
self.wTree=gtk.glade.XML(self.gladefile)
dic={
"on_window1_destory":gtk.main_quit,
"on_btn_about_clicked":self.btn_about_clicked,
......
}
self.wTree.signal_autoconnect(dic)
self.window1=self.wTree.get_widget("window1")
self.about_dialog=self.wTree.get_widget("aboutdialog1")
self.dialog_file=self.wTree.get_widget("dialog_file")
........
class gui_comm:
#失去焦点时获取值
def widget_focus_out_get(self,widget):
self.widget_text=gui_comm().get_view_text(widget)
#return self.widget_text
#设置值
def set_value(self,widget,mytextfile):
#给文本框赋值
self.view_b=widget.get_buffer()
self.view_b.set_text("\n"+mytextfile)
#设置值为弹出框的选择
def set_select(self,widget,dialog):
self.filename=dialog.get_filename()
#print self.filename
dialog.hide()
#给文本框赋值
self.view_b=widget.get_buffer()
self.view_b.set_text(self.filename)
def get_view_text(self,widget):
widget_b=widget.get_buffer()
widget_text=widget_b.get_text(widget_b.get_start_iter(),widget_b.get_end_iter())
return widget_text