簡單檔案壓縮加密指令碼 python

來源:互聯網
上載者:User
#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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.