2010-01-27 16:56:26 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2010-01-27 17:27:11 +03:00
|
|
|
cloak.py - Simple file encryption/compression utility
|
2010-01-27 16:56:26 +03:00
|
|
|
Copyright (C) 2010 Miroslav Stampar, Bernardo Damele A. G.
|
|
|
|
email(s): miroslav.stampar@gmail.com, bernardo.damele@gmail.com
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
|
|
|
|
2010-01-27 17:27:11 +03:00
|
|
|
import bz2
|
2010-01-27 16:56:26 +03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from optparse import OptionError
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
def hideAscii(data):
|
|
|
|
retVal = ""
|
|
|
|
for i in xrange(len(data)):
|
|
|
|
if ord(data[i]) < 128:
|
|
|
|
retVal += chr(ord(data[i]) ^ 127)
|
|
|
|
else:
|
|
|
|
retVal += data[i]
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
def cloak(inputFile):
|
|
|
|
f = open(inputFile, 'rb')
|
2010-01-27 17:27:11 +03:00
|
|
|
data = bz2.compress(f.read())
|
2010-01-27 16:56:26 +03:00
|
|
|
f.close()
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
return hideAscii(data)
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
def decloak(inputFile):
|
|
|
|
f = open(inputFile, 'rb')
|
2010-01-27 17:27:11 +03:00
|
|
|
data = bz2.decompress(hideAscii(f.read()))
|
2010-01-27 16:56:26 +03:00
|
|
|
f.close()
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
return data
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = '%s [-d] -i <input file> [-o <output file>]' % sys.argv[0]
|
|
|
|
parser = OptionParser(usage=usage, version='0.1')
|
|
|
|
|
|
|
|
try:
|
|
|
|
parser.add_option('-d', dest='decrypt', action="store_true", help='Decrypt')
|
|
|
|
parser.add_option('-i', dest='inputFile', help='Input file')
|
|
|
|
parser.add_option('-o', dest='outputFile', help='Output file')
|
|
|
|
|
|
|
|
(args, _) = parser.parse_args()
|
|
|
|
|
|
|
|
if not args.inputFile:
|
|
|
|
parser.error('Missing the input file, -h for help')
|
|
|
|
|
|
|
|
except (OptionError, TypeError), e:
|
|
|
|
parser.error(e)
|
|
|
|
|
2010-01-27 17:27:11 +03:00
|
|
|
if not os.path.isfile(args.inputFile):
|
2010-01-27 16:56:26 +03:00
|
|
|
print 'ERROR: the provided input file \'%s\' is not a regular file' % args.inputFile
|
|
|
|
sys.exit(1)
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
if not args.decrypt:
|
|
|
|
data = cloak(args.inputFile)
|
|
|
|
else:
|
|
|
|
data = decloak(args.inputFile)
|
|
|
|
|
|
|
|
if not args.outputFile:
|
|
|
|
if not args.decrypt:
|
|
|
|
args.outputFile = args.inputFile + '_'
|
|
|
|
else:
|
|
|
|
args.outputFile = args.inputFile[:-1]
|
2010-07-30 16:49:25 +04:00
|
|
|
|
2010-01-27 16:56:26 +03:00
|
|
|
fpOut = open(args.outputFile, 'wb')
|
|
|
|
sys.stdout = fpOut
|
|
|
|
sys.stdout.write(data)
|
|
|
|
sys.stdout.close()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-06-10 01:43:22 +04:00
|
|
|
main()
|