Fix file encryption
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
511458f770
commit
62272039d6
|
@ -51,10 +51,10 @@ def addDoc(args):
|
||||||
args.session = json.load(f)
|
args.session = json.load(f)
|
||||||
|
|
||||||
#Encrypt content
|
#Encrypt content
|
||||||
key, content, nonce = encrypt_file(BASE_DIR + args.file, BASE_DIR + 'encryptedText')
|
key, content = encrypt_file(BASE_DIR + args.file, BASE_DIR + 'encryptedText')
|
||||||
|
|
||||||
#Upload document metadata
|
#Upload document metadata
|
||||||
doc = {'document_name' : args.name, 'key' : key.hex(), 'alg' : 'AES-CFB', 'nonce' : nonce.hex() }
|
doc = {'document_name' : args.name, 'key' : key.hex(), 'alg' : 'AES-CFB' }
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = requests.post(f'http://{state['REP_ADDRESS']}/file/upload/metadata', json=json.dumps(doc),
|
req = requests.post(f'http://{state['REP_ADDRESS']}/file/upload/metadata', json=json.dumps(doc),
|
||||||
|
|
|
@ -12,35 +12,43 @@ def encrypt_file(input_file, output_file=None):
|
||||||
cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
|
cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
|
||||||
encryptor = cipher.encryptor()
|
encryptor = cipher.encryptor()
|
||||||
|
|
||||||
with open(input_file, 'rb') as f:
|
encrypted_content = b""
|
||||||
plaintext = f.read()
|
|
||||||
|
|
||||||
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
|
|
||||||
ciphertext = iv + ciphertext
|
|
||||||
|
|
||||||
if output_file is not None:
|
if output_file is not None:
|
||||||
with open(output_file, 'wb') as f:
|
with open(input_file, 'rb') as infile, open(output_file, 'wb') as outfile:
|
||||||
f.write(ciphertext)
|
# Write the IV to the output file first
|
||||||
|
outfile.write(iv)
|
||||||
|
encrypted_content += iv
|
||||||
|
|
||||||
print(iv.hex())
|
while chunk := infile.read(2048):
|
||||||
|
ciphertext = encryptor.update(chunk)
|
||||||
|
outfile.write(ciphertext)
|
||||||
|
encrypted_content += ciphertext
|
||||||
|
|
||||||
return key, ciphertext, iv
|
# Finalize encryption
|
||||||
|
final_chunk = encryptor.finalize()
|
||||||
|
outfile.write(final_chunk)
|
||||||
|
encrypted_content += final_chunk
|
||||||
|
|
||||||
|
return key, encrypted_content
|
||||||
|
|
||||||
|
|
||||||
# Function to decrypt a file
|
# Function to decrypt a file
|
||||||
def decrypt_file(nonce, key, input_file, output_file=None):
|
def decrypt_file(key, input_file, output_file=None):
|
||||||
with open(input_file, 'rb') as f:
|
with open(input_file, 'rb') as infile:
|
||||||
encrypted_data = f.read()
|
# Read the IV from the input file
|
||||||
|
iv = infile.read(16)
|
||||||
ciphertext = encrypted_data
|
cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
|
||||||
|
|
||||||
cipher = Cipher(algorithms.AES(key), modes.CFB(nonce))
|
|
||||||
decryptor = cipher.decryptor()
|
decryptor = cipher.decryptor()
|
||||||
|
|
||||||
plaintext = decryptor.update(ciphertext) + decryptor.finalize()
|
|
||||||
|
|
||||||
if output_file is not None:
|
if output_file is not None:
|
||||||
with open(output_file, 'wb') as f:
|
with open(output_file, 'wb') as outfile:
|
||||||
f.write(plaintext)
|
while chunk := infile.read(2048):
|
||||||
|
plaintext = decryptor.update(chunk)
|
||||||
|
outfile.write(plaintext)
|
||||||
|
|
||||||
|
# Finalize decryption
|
||||||
|
outfile.write(decryptor.finalize())
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
return plaintext.hex()
|
|
||||||
|
|
|
@ -11,7 +11,6 @@ class File(db_connection.Model):
|
||||||
created_at = db_connection.Column(db_connection.Integer, nullable=False)
|
created_at = db_connection.Column(db_connection.Integer, nullable=False)
|
||||||
key = db_connection.Column(db_connection.String, nullable=False)
|
key = db_connection.Column(db_connection.String, nullable=False)
|
||||||
alg = db_connection.Column(db_connection.String, nullable=False)
|
alg = db_connection.Column(db_connection.String, nullable=False)
|
||||||
nonce = db_connection.Column(db_connection.String, nullable=False)
|
|
||||||
org_id = db_connection.Column(db_connection.Integer, db_connection.ForeignKey('organizations.id'), nullable=False)
|
org_id = db_connection.Column(db_connection.Integer, db_connection.ForeignKey('organizations.id'), nullable=False)
|
||||||
creator_id = db_connection.Column(db_connection.Integer, db_connection.ForeignKey('users.id'), nullable=False)
|
creator_id = db_connection.Column(db_connection.Integer, db_connection.ForeignKey('users.id'), nullable=False)
|
||||||
org = db_connection.relationship('Organization', backref=db_connection.backref('org_files', uselist=False))
|
org = db_connection.relationship('Organization', backref=db_connection.backref('org_files', uselist=False))
|
||||||
|
@ -26,7 +25,6 @@ class File(db_connection.Model):
|
||||||
"created_at": self.created_at,
|
"created_at": self.created_at,
|
||||||
"key": self.key,
|
"key": self.key,
|
||||||
"alg": self.alg,
|
"alg": self.alg,
|
||||||
"nonce": self.nonce,
|
|
||||||
"org": {"id": self.org.id, "name": self.org.name},
|
"org": {"id": self.org.id, "name": self.org.name},
|
||||||
"creator": {"id": self.creator.id, "username": self.creator.username}
|
"creator": {"id": self.creator.id, "username": self.creator.username}
|
||||||
}
|
}
|
|
@ -48,7 +48,7 @@ def file_upload_metadata():
|
||||||
data = request.json
|
data = request.json
|
||||||
if type(data) is str:
|
if type(data) is str:
|
||||||
data = json.loads(data)
|
data = json.loads(data)
|
||||||
if "document_name" not in data or "key" not in data or "alg" not in data or "nonce" not in data:
|
if "document_name" not in data or "key" not in data or "alg" not in data:
|
||||||
return jsonify({"error": "Missing required fields"}), 400
|
return jsonify({"error": "Missing required fields"}), 400
|
||||||
|
|
||||||
org = OrganizationService.get_organization(session.org_id)
|
org = OrganizationService.get_organization(session.org_id)
|
||||||
|
@ -59,7 +59,7 @@ def file_upload_metadata():
|
||||||
if not user:
|
if not user:
|
||||||
return jsonify({"error": "User not found"}), 404
|
return jsonify({"error": "User not found"}), 404
|
||||||
|
|
||||||
file = upload_service.create_file(session.token, org, user, data["document_name"], data["key"], data["alg"], data["nonce"])
|
file = upload_service.create_file(session.token, org, user, data["document_name"], data["key"], data["alg"])
|
||||||
return jsonify(file.to_dict()), 201
|
return jsonify(file.to_dict()), 201
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ class FileService:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.current_requests = {}
|
self.current_requests = {}
|
||||||
|
|
||||||
def create_file(self, session_token: str, org: Organization, user: User, file_name: str, key: str, alg: str, nonce: str) -> File:
|
def create_file(self, session_token: str, org: Organization, user: User, file_name: str, key: str, alg: str) -> File:
|
||||||
file = File(
|
file = File(
|
||||||
file_handle = None,
|
file_handle = None,
|
||||||
document_handle = get_hash(file_name),
|
document_handle = get_hash(file_name),
|
||||||
|
@ -21,7 +21,6 @@ class FileService:
|
||||||
created_at = int(datetime.now().timestamp()),
|
created_at = int(datetime.now().timestamp()),
|
||||||
key = key,
|
key = key,
|
||||||
alg = alg,
|
alg = alg,
|
||||||
nonce = nonce,
|
|
||||||
org_id = org.id,
|
org_id = org.id,
|
||||||
creator_id = user.id,
|
creator_id = user.id,
|
||||||
org = org,
|
org = org,
|
||||||
|
|
Loading…
Reference in New Issue