#!/usr/bin/env python '''zcrypt - File encryption compression / decryption decompression SYNOPSIS zcrypt [-d] key [source [dest]] DESCRIPTION key is the encryption key which is used for encryption and corresponding decryption. source and dest are source and destination file names. If source is - then stdin is used, if dest is - the stdout is used. If dest is omitted then stdout is used. If both source and dest are omitted the stdin and stdout are used. OPTIONS --help Print this documentation. -d Decrypt the source. AUTHOR Written by Stuart Rackham, COPYING Copyright (C) 2000 Stuart Rackham. Free use of this software is granted under the terms of the GNU General Public License (GPL). ''' import sys, os, zlib, rotor '''Version History: 1.0 20 July 2000 Initial release. 1.0.1 22 July 2000 Added --help option. ''' VERSION = "1.0.1" BLK_SIZE = 8*1024 def zcrypt(key, src, dst): '''Compress and encrypt src file object to dst file object. key is the encryption key string.''' r = rotor.newrotor(key) z = zlib.compressobj() while 1: # Compress then encrypt else the randomising effect of encryption will # result in poor compression ratios. s = src.read(BLK_SIZE) if not s: break s = z.compress(s) dst.write(r.encryptmore(s)) dst.write(r.encryptmore(z.flush())) def zdecrypt(key, src, dst): '''Decrypt and decompress src file object to dst file object. key is the encryption key string.''' r = rotor.newrotor(key) z = zlib.decompressobj() while 1: s = src.read(BLK_SIZE) if not s: break s = r.decryptmore(s) dst.write(z.decompress(s)) dst.write(z.flush()) def usage(): cmd = os.path.basename(sys.argv[0]) sys.stderr.write("Usage: %s [-d] [--help] key [srcfile [dstfile]]\n" % cmd) def main(): # Process command line options. import getopt opts,args = getopt.getopt(sys.argv[1:],"d",["help"]) decrypt = 0 for o,v in opts: if o == '--help': print __doc__ sys.exit(0) if o == '-d': decrypt = 1 if len(args) < 1 or len(args) > 3: usage() sys.exit(1) if len(args) == 1 or args[1] == '-': src = sys.stdin else: src = open(args[1],"rb") if len(args) == 2 or args[2] == '-': dst = sys.stdout else: dst = open(args[2],"wb+") if decrypt: zdecrypt(args[0], src, dst) else: zcrypt(args[0], src, dst) src.close() dst.close() if __name__ == "__main__": try: main() except (KeyboardInterrupt, SystemExit): pass except: sys.stderr.write("%s: %s\n" % (os.path.basename(sys.argv[0]), sys.exc_info()[1]))