From 64492c852e3d903526859dcdf16d3ee02732e32b Mon Sep 17 00:00:00 2001 From: EndMove Date: Sat, 12 Mar 2022 18:00:03 +0100 Subject: [PATCH] Correction d'un bug taille de fichier lors de l'envoie storebackend --- .../protocol/rules/reader/SavefileRule.java | 10 +++++--- .../java/lightcontainer/utils/AES_GCM.java | 8 +++--- .../lightcontainer/utils/FileReceiver.java | 14 +++++------ app/src/main/resources/appdata.json | 25 ++++++++++++++++++- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 82aae71..f9afdad 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -11,6 +11,7 @@ import lightcontainer.utils.FileReceiver; import lightcontainer.utils.SHA; import lightcontainer.utils.ShaHasher; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.security.NoSuchAlgorithmException; @@ -41,7 +42,7 @@ public class SavefileRule extends ProtocolReader { public class Result extends ProtocolResult { // Variables private String filename; - private final int size; + private int size; // Construct public Result(Context context, String filename, int size) { @@ -69,8 +70,9 @@ public class SavefileRule extends ProtocolReader { String key = getContext().getAesKey(); String iv = AES_GCM.generateIV(); - if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv)) - throw new IOException(); + // retrieve file and new size + int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv); + if (encryptedFileSize < 0) throw new IOException(); String fileHash = SHA.hashFile(storagePath, this.filename); @@ -80,7 +82,7 @@ public class SavefileRule extends ProtocolReader { getContext().putDataString("iv", iv); getContext().putDataString("fileNameSalt", fileNameSalt); - this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), fileHash), ResultCmdReceiver.STOREBACKEND); + this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND); } catch (IOException | SHA.ShaException e) { this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); e.printStackTrace(); diff --git a/app/src/main/java/lightcontainer/utils/AES_GCM.java b/app/src/main/java/lightcontainer/utils/AES_GCM.java index c7b2dae..4b7f09f 100644 --- a/app/src/main/java/lightcontainer/utils/AES_GCM.java +++ b/app/src/main/java/lightcontainer/utils/AES_GCM.java @@ -44,7 +44,7 @@ public class AES_GCM { encryptStream( new FileInputStream(inFile), new FileOutputStream(outFile), - (int)inFile.length(), + inFile.length(), IVFile, keyFile ); @@ -52,7 +52,7 @@ public class AES_GCM { decryptStream( new FileInputStream(outFile), new FileOutputStream(clearFile), - (int)outFile.length(), + outFile.length(), IVFile, keyFile ); @@ -211,7 +211,7 @@ public class AES_GCM { * * @throws AesGcmException Exception if an error occur. */ - public static void encryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException { + public static void encryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException { byte[] buffer = new byte[1024]; int currentSize = 0; int bytes; @@ -266,7 +266,7 @@ public class AES_GCM { * * @throws AesGcmException Exception if an error occur. */ - public static void decryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException { + public static void decryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException { byte[] buffer = new byte[1024]; int currentSize = 0; int bytes; diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index 50e1b8d..17b7513 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -1,17 +1,13 @@ package lightcontainer.utils; -import javax.crypto.Cipher; -import java.io.BufferedOutputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; public class FileReceiver { private String path; public FileReceiver(String path) { this.path = path; } - public boolean receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { + public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { BufferedOutputStream bosFile = null; try { @@ -21,11 +17,13 @@ public class FileReceiver { bosFile.flush(); bosFile.close(); - return true; + + File f = new File(String.format("%s/%s", path, fileName)); + return (int)f.length(); } catch(IOException | AES_GCM.AesGcmException ex) { ex.printStackTrace(); if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } - return false; + return -1; } } diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 3bc0c1b..372a838 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,24 @@ -{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"D:\\ffe","users":[{"name":"endmove","password":"1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b","aes_key":"p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=","passwordSalt":"ItYiXkwPa84Gwb6dGHQvXQ==","files":[{"name":"096a4b2f5673e1f1e1f4cb7e1a569f06cc31064f5dd254948f5d7c44769fd02a098f48ce60c333fae14cfcb29cacc87e","fileNameSalt":"v9ByBhvxds33+6Ha7c8oPQ==","size":12648,"iv":"OM9l2Rt6dqLDsGS3bQuuEQ==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "226.66.66.1", + "multicast_port": 15502, + "network_interface": "My network interface", + "tls": true, + "storagePath": "D:\\ffe", + "users": [ + { + "name": "endmove", + "password": "1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b", + "aes_key": "p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=", + "passwordSalt": "ItYiXkwPa84Gwb6dGHQvXQ==", + "files": [] + }, + { + "name": "aaaaa", + "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", + "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", + "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", + "files": [] + } + ] +} \ No newline at end of file