zoukankan      html  css  js  c++  java
  • 简单文件压缩加密脚本 python

    #coding:utf-8
    
    # version 1.1
    # 添加了一个参数,可以指定轮换的值,虽然暴力破解完全没难度……
    
    import sys
    import getopt
    import zlib
    import base64
    from 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()
    
  • 相关阅读:
    Educational Codeforces Round 83 --- F. AND Segments
    Educational Codeforces Round 83 --- G. Autocompletion
    SEERC 2019 A.Max or Min
    2019-2020 ICPC Southwestern European Regional Programming Contest(Gym 102501)
    Educational Codeforces Round 78 --- F. Cards
    今天我学习了一门全新的语言
    codeforces 1323D 题解(数学)
    Educational Codeforces Round 80 (Div. 2) 题解 1288A 1288B 1288C 1288D 1288E
    Educational Codeforces Round 81 (Div. 2) 题解 1295A 1295B 1295C 1295D 1295E 1295F
    Codeforces Round #617 (Div. 3) 题解 1296C 1296D 1296E 1296F
  • 原文地址:https://www.cnblogs.com/morya/p/2175076.html
Copyright © 2011-2022 走看看