1+ #!/usr/bin/env python
2+
3+ """
4+ cloak.py - Simple file encryption and/or compression utility
5+ Copyright (C) 2010 Miroslav Stampar, Bernardo Damele A. G.
6+ 7+
8+ This library is free software; you can redistribute it and/or
9+ modify it under the terms of the GNU Lesser General Public
10+ License as published by the Free Software Foundation; either
11+ version 2.1 of the License, or (at your option) any later version.
12+
13+ This library is distributed in the hope that it will be useful,
14+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+ Lesser General Public License for more details.
17+
18+ You should have received a copy of the GNU Lesser General Public
19+ License along with this library; if not, write to the Free Software
20+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+ """
22+
23+ import os
24+ import sys
25+ import bz2
26+
27+ from optparse import OptionError
28+ from optparse import OptionParser
29+
30+ def hideAscii (data ):
31+ retVal = ""
32+ for i in xrange (len (data )):
33+ if ord (data [i ]) < 128 :
34+ retVal += chr (ord (data [i ]) ^ 127 )
35+ else :
36+ retVal += data [i ]
37+
38+ return retVal
39+
40+ def cloak (inputFile ):
41+ retVal = ""
42+
43+ f = open (inputFile , 'rb' )
44+ original = f .read ()
45+ f .close ()
46+
47+ data = bz2 .compress (original )
48+
49+ return hideAscii (data )
50+
51+ def decloak (inputFile ):
52+ retVal = ""
53+
54+ f = open (inputFile , 'rb' )
55+ original = f .read ()
56+ f .close ()
57+
58+ data = bz2 .decompress (hideAscii (original ))
59+
60+ return data
61+
62+ def main ():
63+ usage = '%s [-d] -i <input file> [-o <output file>]' % sys .argv [0 ]
64+ parser = OptionParser (usage = usage , version = '0.1' )
65+
66+ try :
67+ parser .add_option ('-d' , dest = 'decrypt' , action = "store_true" , help = 'Decrypt' )
68+ parser .add_option ('-i' , dest = 'inputFile' , help = 'Input file' )
69+ parser .add_option ('-o' , dest = 'outputFile' , help = 'Output file' )
70+
71+ (args , _ ) = parser .parse_args ()
72+
73+ if not args .inputFile :
74+ parser .error ('Missing the input file, -h for help' )
75+
76+ except (OptionError , TypeError ), e :
77+ parser .error (e )
78+
79+ if args .inputFile == '*' :
80+ pass
81+ elif not os .path .isfile (args .inputFile ):
82+ print 'ERROR: the provided input file \' %s\' is not a regular file' % args .inputFile
83+ sys .exit (1 )
84+
85+ if not args .decrypt :
86+ data = cloak (args .inputFile )
87+ else :
88+ data = decloak (args .inputFile )
89+
90+ if not args .outputFile :
91+ if not args .decrypt :
92+ args .outputFile = args .inputFile + '_'
93+ else :
94+ args .outputFile = args .inputFile [:- 1 ]
95+
96+ fpOut = open (args .outputFile , 'wb' )
97+ sys .stdout = fpOut
98+ sys .stdout .write (data )
99+ sys .stdout .close ()
100+
101+
102+ if __name__ == '__main__' :
103+ main ()
0 commit comments