Encodage de fichier en BASE64 avec grain de sel
Bonjour,
J'ai pour but de créer une application qui permet de crypter les fichiers grâce aux BASE64.
Cependant j'aimerai ajouté un grain de sel pour pouvoir personnalisé cette BASE64 avec qu'il soit plus difficile de le décoder.
Mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCodecBase64 {
public static void main(String args[]) throws Exception {
/* Encode a file and write the encoded output to a text file. */
FileCodecBase64 fileCodecBase64 = new FileCodecBase64();
fileCodecBase64.encode("C:\\Users\\Kentin\\Desktop\\Cahier de charge projet SiiC.pdf",
"C:\\Users\\Kentin\\Desktop\\Cahier de charge projet SiiC_encoded.pdf");
/* Decode a file and write the decoded file to file system */
fileCodecBase64.decode("C:\\Users\\Kentin\\Desktop\\Cahier de charge projet SiiC_encoded.pdf",
"C:\\Users\\Kentin\\Desktop\\Cahier de charge projet SiiC__decoded.pdf");
}
/**
* This method converts the content of a source file into Base64 encoded data
* and saves that to a target file. If isChunked parameter is set to true, there
* is a hard wrap of the output encoded text.
*/
public void encode(String sourceFile, String targetFile) throws Exception {
String saltStr = "123_$@lTàAdd$Up#&_123";
byte[] saltBytes = saltStr.getBytes();
byte[] base64EncodedData = java.util.Base64.getEncoder().encode(loadFileAsBytesArray(sourceFile));
writeByteArraysToFile(targetFile, base64EncodedData);
}
public void decode(String sourceFile, String targetFile) throws Exception {
byte[] decodedBytes = java.util.Base64.getDecoder().decode(loadFileAsBytesArray(sourceFile));
writeByteArraysToFile(targetFile, decodedBytes);
}
/**
* This method loads a file from file system and returns the byte array of the
* content.
*
* @param fileName
* @return
* @throws Exception
*/
public byte[] loadFileAsBytesArray(String fileName) throws Exception {
File file = new File(fileName);
int length = (int) file.length();
BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[length];
reader.read(bytes, 0, length);
reader.close();
return bytes;
}
/**
* This method writes byte array content into a file.
*
* @param fileName
* @param content
* @throws IOException
*/
public void writeByteArraysToFile(String fileName, byte[] content) throws IOException {
File file = new File(fileName);
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
writer.write(content);
writer.flush();
writer.close();
}
} |
J'aimerai ajouter un tableau de bytes supplémentaire lors de l'encodage et l'enlevé lors du décodage afin qu'il ne soit pas décodable par n'importe qui.
Je sais pas trop où et comment ajouter mon propre tableau de bytes.
Merci d'avance,
Et bonne journée ! :zoubi: