#!/usr/bin/env python3
-""" Encrypt a single file
+""" Encrypt/decrypt a single file
low-cost quick-devel badly-documented
return crypto.Decrypt (password=password, key=key)
-def main(do_encrypt, in_file, out_file, password, comptype='gz', enctype='aes',
+def main(do_encrypt, in_file, out_file, password, comptype='gz',
bufsize=tarfile.RECORDSIZE, encoding='UTF-8'):
""" Main function, called when running file as script
try:
# open file to read
if do_encrypt:
- read_handle = open(in_file, 'rt')
+ read_handle = open(in_file, 'rb')
else:
decryptor = initialize_encryption (CRYPTO_MODE_DECRYPT,
password=password)
comptype=comptype, bufsize=bufsize,
fileobj=None, encryption=encryptor)
else:
- write_handle = open(out_file, 'wt')
+ write_handle = open(out_file, 'wb')
return_code = 1
# convert
+ total = 0
while True:
buf = read_handle.read(bufsize)
+ total += len(buf)
print('.', end='')
if do_encrypt:
- write_handle.write(buf.encode(encoding, errors='strict'))
+ write_handle.write(buf)
else:
- write_handle.write(buf.decode(encoding, errors='replace'))
+ # write_handle.write(buf.decode(encoding, errors='replace'))
+ write_handle.write(buf)
if len(buf) < bufsize:
if do_encrypt:
print('successfully encrypted {} into {}'
if __name__ == '__main__':
if len(sys.argv) < 4:
- print('file_crypt.py {enc|dec} [-c] INFILE OUTFILE PASSWORD')
+ print('file_crypt.py [-c] {enc|dec} INFILE OUTFILE PASSWORD')
sys.exit(2)
elif sys.argv[1] == '-h':
- print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD')
+ print('file_crypt.py [-c] {enc|dec} INFILE OUTFILE PASSWORD')
sys.exit(0)
comptype = 'tar' # auto-detect, not sure whether this would work
elif sys.argv[idx] == 'dec':
do_encrypt = False
else:
- print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD')
+ print('file_crypt.py [-c] {enc|dec} INFILE OUTFILE PASSWORD')
sys.exit(2)
if len(sys.argv) != idx+4:
- print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD')
+ print('file_crypt.py [-c] {enc|dec} INFILE OUTFILE PASSWORD')
sys.exit(2)
sys.exit(main(do_encrypt, sys.argv[idx+1], sys.argv[idx+2],