Bonjours à tous !

Je rencontre un problème assez pénible. Je développe un logiciel qui utilise la cryptographie. Celui ci crypte une string avant d'écrire la string cryptée dans un fichier .txt . Cependant, je rencontre un problème à la lecture.

L'algorithme utilisé est AES.

Voici mon code :

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
Imports System.IO
Imports System.Security.Cryptography
 
Public Class Client
 
#Region "Variables"
 
    Dim Counter1 As Integer = 0
    Dim path As String = "J:\test.txt"
    Dim myAes As Aes = Aes.Create()
 
#End Region
 
#Region "Procédures"
 
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        crypt()
    End Sub
 
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        decrypt()
    End Sub
 
#End Region
 
#Region "CRYPT"
 
    Private Sub crypt()
 
 
        Dim sw As StreamWriter = New StreamWriter(path, False)
 
 
        For i = 0 To ListBox1.Items.Count - 1 Step 1
 
                Dim original As String = AssemblyTextForCrypt(i)
                Dim encrypted As Byte() = EncrypText(original, myAes.Key, myAes.IV)
                Dim Final As String = System.Text.Encoding.Unicode.GetString(encrypted)
                sw.WriteLine(Final)
            Next
            sw.Flush()
        sw.Close()
    End Sub
 
    Function AssemblyTextForCrypt(ByVal index As Integer)
 
        Dim assembly As String = ""
        assembly = ListBox1.Items.Item(index) + "|" + ListBox2.Items.Item(index) + "|" + ListBox3.Items.Item(index) + "|" + ListBox4.Items.Item(index)
        Return assembly
 
    End Function
 
    Function EncrypText(ByVal Text As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
 
        Dim encrypted() As Byte
        ' Create an Aes object
        ' with the specified key and IV.
 
        ' Create a decrytor to perform the stream transform.
        Dim encryptor As ICryptoTransform = myAes.CreateEncryptor(myAes.Key, myAes.IV)
        ' Create the streams used for encryption.
        Using msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)
 
                    'Write all data to the stream.
                    swEncrypt.Write(Text)
                End Using
                encrypted = msEncrypt.ToArray()
            End Using
        End Using
 
        ' Return the encrypted bytes from the memory stream.
        Return encrypted
 
    End Function
 
#End Region
 
#Region "DECRYPT"
 
    Sub decrypt()
 
        Dim sr As StreamReader = New StreamReader(path)
        Dim ligne() As Byte
 
        Do
            ligne = System.Text.Encoding.Unicode.GetBytes(sr.ReadLine)
            Dim decrypted As String = DecrypText(ligne, myAes.Key, myAes.IV)
            MsgBox(ligne.ToString)
            Dim List() As String = DesassemblyForRead(decrypted)
            Place(List)
        Loop Until ligne Is Nothing
        sr.Close()
 
    End Sub
 
    Function DecrypText(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
        ' Declare the string used to hold
        ' the decrypted text.
        Dim plaintext As String = Nothing
 
        ' Create an Aes object
        ' with the specified key and IV.
 
        ' Create a decrytor to perform the stream transform.
        Dim decryptor As ICryptoTransform = myAes.CreateDecryptor(myAes.Key, myAes.IV)
 
        ' Create the streams used for decryption.
        Using msDecrypt As New MemoryStream(cipherText)
 
            Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
 
                Using srDecrypt As New StreamReader(csDecrypt)
 
                    ' Read the decrypted bytes from the decrypting stream
                    ' and place them in a string.
                    plaintext = srDecrypt.ReadToEnd
 
                End Using
            End Using
        End Using
 
        Return plaintext
 
    End Function 'DecryptStringFromBytes_Aes 
 
 
    Function DesassemblyForRead(ByVal line As String)
 
        Dim List() As String = Nothing
        List = line.Split("|")
        Return List
 
    End Function
 
    Sub Place(ByVal Arr() As String)
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        ListBox3.Items.Clear()
        ListBox4.Items.Clear()
        ListBox1.Items.Add(Arr(0))
        ListBox2.Items.Add(Arr(1))
        ListBox3.Items.Add(Arr(2))
        ListBox4.Items.Add(Arr(3))
    End Sub
 
#End Region

Le cryptage et l'écriture se déroule sans problème.

J'ai une erreur du type : System.Security.Cryptography.CryptographicException: Le remplissage n'est pas valide et ne peut pas être supprimé.
sur la ligne

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
 plaintext = srDecrypt.ReadToEnd
J'ai essayer de retourner un tableau de bytes au lieu d'une string mais le problème persiste.

N'étant pas expert en la matière, j'ai du mal a voir d'ou vient cette erreur.

NB: J'ai déjà cherché sur google, mais je n'ai rien trouvé qui fonctionne ou qui puisse m'aider.

Merci de votre aide !

Ind6x