简单文件压缩加密脚本 python
2011-09-13 20:39
676 查看
#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()
相关文章推荐
- 编写一个Python脚本——Windows下Python压缩文件
- 分享一个简单的python读写文件脚本
- Python:简单的文件备份脚本
- 一个简单的python读写文件脚本
- 简单编写的文件复制模块python脚本
- 解压文件夹中的压缩文件 Python脚本
- 自动解压大量压缩文件 Python 脚本
- 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
- 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
- 使用一个简单的python脚本将一个本地文件以码流的形式,通过UDP协议发送到对端:
- (windows)python脚本:自动备份并压缩文件,同时删除过期文件
- python 简单备份文件脚本v1.0的实例
- (windows)python脚本:自动备份并压缩文件,同时删除过期文件
- 文件批量加密重命名--python脚本AND mysql命令行导入数据库
- 学习笔记:zipfile的简单使用解压和压缩文件,以为处理加密的情况
- Python实现简单的文件传输与MySQL备份的脚本分享
- Python中使用gzip模块压缩文件的简单教程
- 一个简单的压缩成tar.gz文件的shell脚本
- Python实现简单的文件传输与MySQL备份的脚本分享
- Python绝技笔记--------Zip压缩文件密码爆破小脚本