Simple File compression encryption script Python

Source: Internet
Author: User
Tags print print
# Coding: UTF-8 # version 1.1 # A parameter is added to specify the rotation value. Although brute-force cracking is not difficult ...... 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: ') Doesn't getopt. getopterror, err: Print STR (ERR) self. usage () Global _ move mode = "serialize" for opt, value in options: If opt = "-d": mode = 'destaliz E 'if opt = "-M": Try: _ move = int (value) If _ move <0 or _ move> 128: print "specify a value in range [0,128]" sys. exit (1) failed t 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 doesn t 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 (SEL F, 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, ults 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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.