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
| package fr.akwaa.textencryptor.encryptor;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Créé le 23/01/2022 à 17h44 par valen
**/
public class EncryptedFiles {
public static void createEncryptedFile(String encryotedFileName, String content){
File encryptedFile = new File("EncryptedFiles/" + encryotedFileName + ".encrypted");
try{
if(!encryptedFile.exists()){
if(!encryptedFile.getParentFile().exists() && !encryptedFile.getParentFile().mkdir()) return;
if(!encryptedFile.createNewFile()) return;
}
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(encryptedFile), StandardCharsets.UTF_16);
byte[] contentBytes = content.getBytes();
for(byte contentByte : contentBytes) outputStreamWriter.write(contentByte);
outputStreamWriter.flush();
outputStreamWriter.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void deleteEncryptedFile(String encryptedFileName){
File encryptedFile = new File("EncryptedFiles/" + encryptedFileName + ".encrypted");
if(!encryptedFile.exists()) return;
if(!encryptedFile.delete()){
try{
throw new IOException();
}catch(IOException e){
e.printStackTrace();
}
}
}
public static List<String> getEncryptedFilesName(){
List<String> encryptedFilesName = new ArrayList<>();
File encryptedFilesFolder = new File("EncryptedFiles/");
if(!encryptedFilesFolder.exists()) return encryptedFilesName;
for(File encryptedFile : Objects.requireNonNull(encryptedFilesFolder.listFiles())){
encryptedFilesName.add(encryptedFile.getName().substring(0, encryptedFile.getName().lastIndexOf(".encrypted")));
}
return encryptedFilesName;
}
public static String getEncryptedFileContent(String encryptedFileName){
StringBuilder fileContentBuilder = new StringBuilder();
try{
File encryptedFile = new File("EncryptedFiles/" + encryptedFileName + ".encrypted");
if(!encryptedFile.exists()) return "";
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(encryptedFile), StandardCharsets.UTF_16);
int n;
while((n = inputStreamReader.read()) != -1){
fileContentBuilder.append((char) n);
}
inputStreamReader.close();
}catch(IOException e){
e.printStackTrace();
}
return fileContentBuilder.toString();
}
} |
Partager