Bonjour, j'ai créé un projet TextEncryptor qui permet de crypter du texte avec une clef qui est ensuite stocké dans un fichier, puis de le décrypter si on met la même clef. Tout marche, le texte se crypte dans un fichier sauf qu'en le décryptant j'obtient ^^^^ à la place de mes lettres accentués (ici le 'é'). Qui pourrait m'aider (code relatif à ce problème ci-dessous)

fr.akwaa.textencryptor.encryptor.EncryptedFiles :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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();
    }
}
fr.akwaa.textencryptor.encryptor.Encryptor :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
package fr.akwaa.textencryptor.encryptor;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Créé le 23/01/2022 à 14h31 par valen
 **/
 
public class Encryptor {
    public static String encrypt(String input, String key){
        String binaryInput = toBinaryString(input);
        String binaryKey = toBinaryString(key);
 
        List<String> encryptedBytes = new ArrayList<>();
        StringBuilder currentByteBuilder = new StringBuilder();
        int currentBitIndex = 0;
        int keyIndex = 0;
        for(char c : binaryInput.toCharArray()){
            if(Integer.parseInt(String.valueOf(c)) + Integer.parseInt(String.valueOf(binaryKey.charAt(keyIndex))) == 1){
                currentByteBuilder.append("1");
            }else{
                currentByteBuilder.append("0");
            }
            currentBitIndex++;
            if(currentBitIndex >= 8){
                encryptedBytes.add(currentByteBuilder.toString());
                currentByteBuilder = new StringBuilder();
                currentBitIndex = 0;
            }
            keyIndex++;
            if(keyIndex >= binaryKey.length()) keyIndex = 0;
        }
 
        StringBuilder output = new StringBuilder();
        for(String encryptedByte : encryptedBytes){
            output.append(Character.valueOf((char) Integer.parseInt(encryptedByte, 2)).toString());
        }
 
        return output.toString();
    }
 
    private static String toBinaryString(String string){
        StringBuilder binaryStringBuilder = new StringBuilder();
 
        for(Byte b : string.getBytes()){
            String binaryString = Integer.toBinaryString(b);
            binaryStringBuilder.append("0".repeat(Math.max(0, 8 - binaryString.length())));
            binaryStringBuilder.append(binaryString);
        }
 
        return binaryStringBuilder.toString();
    }
}