#coding:utf-8# version 1.1# 添加了一個參數,可以指定輪換的值,雖然暴力破解完全沒難度……import sysimport getoptimport zlibimport base64from cStringIO import StringIO_move = 15_line_sep = "\n"class TMain: def __init__(self): options, args = 0,0 if len(sys.argv) == 1: self.usage() try: options, args = getopt.getopt(sys.argv[1:], 'dm:') except getopt.GetoptError, err: print str(err) self.usage() global _move mode = "serialize" for opt, value in options: if opt == "-d": mode = 'deSerialize' if opt == "-m": try: _move = int(value) if _move < 0 or _move > 128: print "Specify a value in range [0, 128]" sys.exit(1) except ValueError: # won't effect _move pass for filename in args: f = getattr(self, mode) s = f(self.readfile(filename)) fout = file(filename + ".out.py", 'wb') fout.write(s) fout.close() print print "##################################" print s def moveCode(self, strobj ): result = "" for i in strobj: pass def compress(self,strobj): return zlib.compress(strobj, 9) def decompress(self,strobj): return zlib.decompress(strobj) def serialize(self, strobj ): '''serialize the string into encoded format''' strobj = self.compress( strobj ) strobj = base64.standard_b64encode(strobj) max_char_per_line = 40 c = 0 result = "" for i in strobj: c += 1 if c >= max_char_per_line: c = 1 result += _line_sep result += hex( ord( i ) #+ _move )[2:] return result def deSerialize(self, strobj ): strobj = strobj.replace( _line_sep, '' ) result = "" strlen = len(strobj) if strlen % 2 != 0 : raise Exception("Bad value") for i in xrange( 0, len( strobj ), 2 ): c = strobj[i:i + 2] try: c = chr( int( c, 16 ) #- _move ) tmp= hex( ord(c))[2:] result += c except ValueError, err: print c, int( c, 16 ), int( c, 16 ) - _move # for i in result: # sys.stdout.write(i) # print result = base64.standard_b64decode(result) #return compress(result) return self.decompress( result ) def readfile(self, filename ): return open( filename ).read() def usage(self,): usage_str= '''{this_program} [-d] [-m 10] filename if the -d option is given, program will try to decode file, otherwise, program will encode file and print the result. -m specifies the move steps for character, defaults to 15, notice, remember this value! you will need it'''.format( this_program = sys.argv[0]) print usage_str sys.exit()def main(): m = TMain()if __name__ == '__main__': main()