Bonjour,

J'ai récupéré et amélioré et transformé en AES ce code de chiffrement que j'ai trouvé sur le site de Microsoft (https://support.microsoft.com/fr-fr/kb/301070).

Cependant, il passait obligatoirement par deux fichiers, et je cherchais un moyen de chiffrer simplement un texte variable pour le mettre dans un fichier afin de le sauvegarder. J'ai crée le fichier au début contenant le texte variable mais cela pose beaucoup de problèmes de sécurité de créer un fichier brut, sans chiffrement, pour le chiffrer, et le supprimer après, surtout si la suppression n'a pas lieu.

Je suis donc arrivé au code ci-dessous, tout marche bien, sauf la partie après le Else dans la fonction déchiffrement, je vous ais donné les résultats de mes deux tests.

Si vous pouviez m'aider à récupérer le texte contenu dans le fichier correctement. Merci beaucoup par avance et merci pour l'attention porté à mon message.

PS : Au besoin,
sourcefile : Contient un chemin d'accès en chaîne de charactères vers un fichier si file = True ou une chaîne de charactères basique si file = False
resultfile : Contient toujours un chemin d'accès vers un fichier
key : Le résultat d'un hachage de la clé, 32 caractères
IV : L'initialisation, 16 charactères
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
 
Function aesencryptor(ByVal sourcefile As String, ByVal resultfile As String, ByVal key As String, ByVal IV As String, Optional ByVal file As Boolean = False)
        Try
            Dim myDESProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider
 
            myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes(key)
            myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes(IV)
            Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateEncryptor
            If file = True Then
                Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
                Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
                Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
 
                Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
                ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.Close()
                ProcessFileStream.Close()
                ResultFileStream.Close()
            Else
                Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
                Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
                myCryptoStream.Write(ASCIIEncoding.ASCII.GetBytes(sourcefile), 0, ASCIIEncoding.ASCII.GetBytes(sourcefile).Length)
                myCryptoStream.Close()
                ResultFileStream.Close()
            End If
 
        Catch ex As Exception
            Console.WriteLine(ex.Message)
 
        End Try
 
    End Function
    Function aesdecryptor(ByVal sourcefile As String, ByVal resultfile As String, ByVal key As String, ByVal IV As String, Optional ByVal file As Boolean = False)
        Try
            Dim myDESProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider
 
            myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes(key)
            myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes(IV)
            Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryptor
            If file = True Then
                Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
                Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
                Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
 
                Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
                ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.Close()
                ProcessFileStream.Close()
                ResultFileStream.Close()
            Else
                Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
                Dim fileflux As Stream = New MemoryStream()
                Dim myCryptoStream As CryptoStream = New CryptoStream(fileflux, myICryptoTransform, CryptoStreamMode.Write)
                Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
                ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
                MsgBox(bytearrayinput.Length) 'Résultat : 16
                myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.FlushFinalBlock()
                Dim plain(fileflux.Length - 1) As Byte
                fileflux.Read(plain, 0, plain.Length)
                MsgBox(plain.Length) 'Résultat : 13
                MsgBox(ASCIIEncoding.ASCII.GetString(plain)) 'Mais rien ici, msgbox vide
                ProcessFileStream.Close()
                fileflux.Close()
                Return ASCIIEncoding.ASCII.GetString(plain)
            End If
 
        Catch ex As Exception
            Console.WriteLine(ex.Message)
 
        End Try
 
    End Function
Partie problématique :
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
 
Else
                Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
                Dim fileflux As Stream = New MemoryStream()
                Dim myCryptoStream As CryptoStream = New CryptoStream(fileflux, myICryptoTransform, CryptoStreamMode.Write)
                Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
                ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
                MsgBox(bytearrayinput.Length) 'Résultat : 16
                myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
                myCryptoStream.FlushFinalBlock()
                Dim plain(fileflux.Length - 1) As Byte
                fileflux.Read(plain, 0, plain.Length)
                MsgBox(plain.Length) 'Résultat : 13
                MsgBox(ASCIIEncoding.ASCII.GetString(plain)) 'Mais rien ici, msgbox vide
                ProcessFileStream.Close()
                fileflux.Close()
                Return ASCIIEncoding.ASCII.GetString(plain)
            End If
PS : Ne prenez pas en compte le nom DES des variables, j'ai eu un peu la flemme de les changer