From 6cb59673f4df70acc398b73dbba4161142457146 Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 15 Mar 2022 19:18:57 +0100 Subject: [PATCH] =?UTF-8?q?Correction=20bug=20encryption=20fichier=20(tail?= =?UTF-8?q?le)=20correction=20bug=20d=C3=A9cryption=20ficher=20(taille)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protocol/rules/reader/RetrieveOkRule.java | 7 ++++++- .../protocol/rules/writer/GetFileOkRule.java | 11 +++++++++-- app/src/main/java/lightcontainer/utils/AES_GCM.java | 10 ++++++---- .../main/java/lightcontainer/utils/FileReceiver.java | 5 +---- .../main/java/lightcontainer/utils/FileSender.java | 3 --- app/src/main/resources/appdata.json | 10 +--------- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java index 412681e..226e2f5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -55,7 +55,7 @@ public class RetrieveOkRule extends ProtocolReader { FileReceiver fileReceiver = new FileReceiver(storagePath); fileReceiver.receiveFile(reader, this.filename, this.filesize); - // TODO fingerprint + // TODO fingerprint } } @@ -68,6 +68,11 @@ public class RetrieveOkRule extends ProtocolReader { @Override protected RetrieveOkRule.Result onExecuted(Context context, String... data) { RetrieveOkRule.Result result = new RetrieveOkRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]); + + // save encrypted file size into bundle + context.putDataInt("encryptedFileSize", Integer.parseInt(data[FILE_SIZE])); // TODO to long ?! + + // Set result command result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT); return result; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java index 08390bb..cf3c156 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -36,7 +36,7 @@ public class GetFileOkRule extends ProtocolWriter { } /** - * Oh yeahh baby + * Oh yeahh baby by tonton EndMove ;-) * @param writer Buffer à remplir qui sera envoyer via le réseau */ @Override @@ -44,8 +44,15 @@ public class GetFileOkRule extends ProtocolWriter { super.write(writer); System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize); + System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize")); FileSender fileSender = new FileSender(storagePath); - fileSender.sendFile(getContext().getHashedFileName(this.filename), writer, this.filesize, getContext().getAesKey(), getContext().getDataString("fileIV")); + fileSender.sendFile( + getContext().getHashedFileName(this.filename), + writer, + getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) + getContext().getAesKey(), + getContext().getDataString("fileIV") + ); } } diff --git a/app/src/main/java/lightcontainer/utils/AES_GCM.java b/app/src/main/java/lightcontainer/utils/AES_GCM.java index 4b7f09f..020d5de 100644 --- a/app/src/main/java/lightcontainer/utils/AES_GCM.java +++ b/app/src/main/java/lightcontainer/utils/AES_GCM.java @@ -202,10 +202,11 @@ public class AES_GCM { /** * Encrypt stream, with AES GCM. + * The output stream is automatically closed after processing. * * @param in InputStream to the input, flux to encrypt. * @param out OutputStream to the output, encrypted flux. - * @param fileSize Stream/file size. + * @param fileSize Stream/file size (! unencrypted size !). * @param key Base64 encoded secret key. * @param IV Base64 encoded vector. * @@ -213,7 +214,7 @@ public class AES_GCM { */ public static void encryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException { byte[] buffer = new byte[1024]; - int currentSize = 0; + long currentSize = 0; int bytes; try { // Make the cipher for encryption @@ -257,10 +258,11 @@ public class AES_GCM { /** * Decrypt stream, with AES GCM. + * The input stream is automatically closed after processing. * * @param in InputStream to the input, flux to decrypt. * @param out OutputStream to the output, decrypted flux. - * @param fileSize Stream/file size. + * @param fileSize Stream/file size (! encrypted size !). * @param key Base64 encoded secret key. * @param IV Base64 encoded vector. * @@ -268,7 +270,7 @@ public class AES_GCM { */ public static void decryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException { byte[] buffer = new byte[1024]; - int currentSize = 0; + long currentSize = 0; int bytes; try { // Make the cipher for decryption diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index fd6fc65..826bb1f 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -15,11 +15,8 @@ public class FileReceiver { AES_GCM.encryptStream(input, bosFile, fileSize, key, iv); - bosFile.flush(); - bosFile.close(); - File f = new File(String.format("%s/%s", path, fileName)); - return (int)f.length(); + return (int)f.length(); // TODO change the size to LONG } catch(IOException | AES_GCM.AesGcmException ex) { ex.printStackTrace(); if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index bc58fc5..4d19ad8 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -15,10 +15,7 @@ public class FileSender { File f = new File(String.format("%s/%s", path, filename)); if(f.exists()) { bisFile = new BufferedInputStream(new FileInputStream(f)); - AES_GCM.decryptStream(bisFile, out, fileSize, aesKey, iv); - - bisFile.close(); return true; } else return false; diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index aed2e08..fe20e77 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1,9 +1 @@ -{ - "unicast_port": 8000, - "multicast_ip": "224.66.66.1", - "multicast_port": 15502, - "network_interface": "lo", - "tls": true, - "storagePath": "D:\\ffe", - "users": [] -} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"lo","tls":true,"storagePath":"D:\\ffe","users":[{"name":"aaaaa","password":"$2a$10$639SgWZgYr9nfwEkruQUae8Y0ibNwa47IjWXOeNYjq5Cdc.AssyBW","aes_key":"oy+C8i9azdqof9/o4Do/karqP0bnLp3kBMAs/LW3Xn4=","files":[{"name":"dupli.zip","fileNameSalt":"vtI75a/mh9voNohJW6Or5A==","size":41264222,"iv":"Xj4QHP/dzLYCRNqfuYB4hw==","storage":["orglightcont01"]}]}]} \ No newline at end of file