56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
#!/usr/bin/env python3.5
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Basic file-to-file and string-to/from-file cryptographic module.
|
|
"""
|
|
|
|
|
|
from simplecrypt import encrypt, decrypt, DecryptionException
|
|
|
|
|
|
__authors__ = "Tatiyk, HorlogeSkynet"
|
|
__copyright__ = "Copyright 2017, ACMS"
|
|
__license__ = "GPLv3"
|
|
__status__ = "Production"
|
|
__date__ = "03/04/2017"
|
|
|
|
|
|
# This function does not remove the original file. The caller has to.
|
|
def cryptFileToFile(source, key, destination):
|
|
with open(source, 'rb') as source:
|
|
with open(destination, 'wb') as destination:
|
|
destination.write(encrypt(key, source.read()))
|
|
|
|
|
|
# This function does not remove the encrypted file. The caller has to.
|
|
def decryptFileToFile(source, key, destination):
|
|
with open(source, 'rb') as source:
|
|
with open(destination, 'wb') as destination:
|
|
temp = source.read()
|
|
|
|
try:
|
|
data = decrypt(key, temp)
|
|
|
|
except DecryptionException:
|
|
return False
|
|
|
|
else:
|
|
destination.write(data)
|
|
|
|
|
|
def cryptStringToFile(data, key, destination):
|
|
with open(destination, 'wb') as destination:
|
|
destination.write(encrypt(key, data.encode('utf-8')))
|
|
|
|
|
|
def decryptFileToString(source, key):
|
|
with open(source, 'rb') as source:
|
|
temp = source.read()
|
|
|
|
try:
|
|
return decrypt(key, temp).decode('utf-8')
|
|
|
|
except DecryptionException:
|
|
return False
|